フィルターのクリア

How can I make a loop inside a loop k time

1 回表示 (過去 30 日間)
muly san
muly san 2013 年 8 月 15 日
For example:
I want to create fonction that get matrix: A=[3 5 6]
and do the next steps:
for i1=1:A(1); for i2=1:A(2); for i3=1:A(3); B=[i1 i2 i3]; end end end
But the length of A can vary. thank you

回答 (2 件)

Andrei Bobrov
Andrei Bobrov 2013 年 8 月 15 日
編集済み: Andrei Bobrov 2013 年 8 月 15 日
Use function fullfact from Statistics Toolbox
B = fullfact(A);
or
a = arrayfun(@(x)1:x,A,'un',0);
[i3,i2,i1] = ndgrid(a{end:-1:1});
B = [i1(:),i2(:),i3(:)];
or
B = zeros(prod(A),3);
k = 1;
for i1 = 1:A(1)
for i2 = 1:A(2)
for i3 = 1:A(3)
B(k,:) = [i1,i2,i3];
k = k +1;
end
end
end

muly san
muly san 2013 年 8 月 15 日
thank you.
It was just an example. I want to know how to do the loop inside a loop k time anyway.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by