How to code with for loop loops without using ndgrid?
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, how do I write the following code without ndgrid, that is, with for loop loops, without using a ready-made library.
[A B C]=ndgrid(1:2,3:4,5:6);
A1=reshape(A,[],1);
B1=reshape(B,[],1);
C1=reshape(C,[],1);
d=[A1,B1,C1]
0 件のコメント
回答 (2 件)
David Hill
2021 年 10 月 27 日
count=0;
for c=5:6
for b=3:4
for a=1:2
count=count+1;
d(count,:)=[a,b,c];
end
end
end
0 件のコメント
Stephen23
2021 年 10 月 27 日
As this is clearly homework, this should get you started:
inp = {1:2, 3:4, 5:6};
[A,B,C] = ndgrid(inp{:});
A1=reshape(A,[],1);
B1=reshape(B,[],1);
C1=reshape(C,[],1);
d=[A1,B1,C1]
S = cellfun(@numel,inp);
reshape(repmat(reshape(inp{1},S(1),1,1),1,S(2),S(3)),[],1)
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!