Help creating a for loop

1 view (last 30 days)
Noah Wilson
Noah Wilson on 12 Jun 2019
Answered: Walter Roberson on 12 Jun 2019
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.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jun 2019
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))

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!