Help creating a for loop

1 visualizzazione (ultimi 30 giorni)
Noah Wilson
Noah Wilson il 12 Giu 2019
Risposto: Walter Roberson il 12 Giu 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.

Risposta accettata

Walter Roberson
Walter Roberson il 12 Giu 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))

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by