For loop with horzcat
古いコメントを表示
Why does this not success?? Would someone help me correct the code?
A=[1 5; 3 3; 8 6; 4 8; 9 14; 11 11; 13 12; 15 7; 17 15; 1 20]
B=[]
for i=1:5
for j=1:5
B(i,:)=horzcat(B(i,:),A(i+j-1,:));
end
end
I'd like to get this
B=[1 5 3 3 8 6 4 8 9 14;3 3 8 6 4 8 9 14 11 11; 8 6 4 8 9 14 11 11 13 12; 4 8 9 14 11 11 13 12 15 7;9 14 11 11 13 12 15 7 17 15;11 11 13 12 15 7 17 15 1 20]
採用された回答
その他の回答 (1 件)
horzcating in for-loops is bad practice. Better to do,
AA=reshape(A.',[],1).';
B=AA( (1:10)+(0:2:10).' )
2 件のコメント
Ryosuke Saito
2020 年 4 月 12 日
Matt J
2020 年 4 月 12 日
My solution is independent of the contents of A.
カテゴリ
ヘルプ センター および 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!