Help creating a for loop

1 回表示 (過去 30 日間)
Noah Wilson
Noah Wilson 2019 年 6 月 12 日
回答済み: Walter Roberson 2019 年 6 月 12 日
I am taking some data in a 28x6 matrix and I am wanting to create a for loop that takes the value in each position in and divides that value by the sum of the entire row and then replace said value with that new calculated value. Then do this for each value in the matrix.
For example if I have the matrix:
[1 2 3 ; 4 5 6]
I would like to create a loop that makes the following matrix from the above matrix:
[(1/(1+2+3)) (2/(1+2+3)) (3/(1+2+3)) ; (4/(4+5+6)) (5/(4+5+6)) (6/(4+5+6))]
I am not sure how to go about this in a for loop and would appreciate the help.

採用された回答

Walter Roberson
Walter Roberson 2019 年 6 月 12 日
M = [1 2 3 ; 4 5 6];
M ./ sum(M,2)
Note: this code requires R2016b or later. For earlier versions,
bsxfun(@rdivide, M, sum(M,2))

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by