How to iterate through matrix with rows and columns changing by the same value?
8 ビュー (過去 30 日間)
古いコメントを表示
Savva Cherdantsev
2017 年 8 月 4 日
コメント済み: Savva Cherdantsev
2017 年 8 月 4 日
Hello, All! I have 3x3 matrix A:
A=[2 3 7;5 6 8;1 9 3]
I want to make a 3x1 matrix B, which will consist of the maximum value of each column of matrix A. Here is my loop:
for row=1:size(A,1)
for col=1:size(A,2)
B(row,1)=max(A(:,col))
end
end
The code does not work and if I debug, I see that when I am iterating through loops:
The first iteration:
B(1,1)=max(A(:,1)), which is fine
The second iteration:
B(1,1)=max(A(:,2)), which is not fine, as the row for B did not change from 1 to 2.
Question:
How to change my code? Is there a way to iterate through two for loops by changing rows and columns by the same value?
0 件のコメント
採用された回答
Andrei Bobrov
2017 年 8 月 4 日
編集済み: Andrei Bobrov
2017 年 8 月 4 日
B = max(A)';
or
B = zeros(size(A,2),1);
for col=1:size(A,2)
B(col,1)=max(A(:,col));
end
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!