Help creating a for loop

1 Ansicht (letzte 30 Tage)
Noah Wilson
Noah Wilson am 12 Jun. 2019
Beantwortet: Walter Roberson am 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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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))

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by