For loop not working
1 回表示 (過去 30 日間)
古いコメントを表示
I have a 2x4 matrix called A. I would like to find the sum of each column and divide each element in that column with the sum of that column. However, when I run the loop, the first 3 columns have no values in them. How do I rectify this? The following is my code:
A = rand(2,4)
for i=length(A)
B(:,i) = A(:,i)./sum(A(:,1));
end
0 件のコメント
採用された回答
VBBV
2022 年 6 月 6 日
編集済み: VBBV
2022 年 6 月 6 日
A = rand(2,4)
for i=1:length(A) % using a single value
B(:,i) = A(:,i)./sum(A(:,i)); % divide each element with sum of that column
end
B
It seems you are using a single value in loop counter
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!