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 CenterFile Exchange에서 Creating 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