Create all combinations of a vector
古いコメントを表示
Hello,
I need your help to write down the different possible combinations of a vector x used as input for my model.
Basically I have a vector x of i elements that can take the values 0 or 1. The subset of the y last elements of x is "mutually exclusive", which means that only 1 of those elements at a time can have a value of 1.
For every value of x I run my model and then save the results in a file.
Until now I have nested as many for-loops as there are elements in x to depict every case, but I suspect there must be a much more convenient way to do this!
So basically I would like to improve following code (it's ok if you laugh seeing it! :P) and add the mutually exclusive condition:
%% Create the x vector for all possible combinations
for m=1:2
x(1)=m-1;
for n=1:2
x(2)=n-1;
for o=1:2
x(3)=o-1;
for p=1:2
x(4)=p-1;
for q=1:2
x(5)=q-1;
for r=1:2
x(6)=r-1;
for s=1:2
x(7)=s-1;
for t=1:2
x(8)=t-1;
for u=1:2
x(9)=u-1;
for v=1:2
x(10)=v-1;
%% Run the model
[A,B]=Zmultipleexecution(x);
%% Save the results in a .mat file
file=['C:\Users\XXX\Documents\',num2str(m)-1,num2str(n)-1,num2str(o)-1,num2str(p)-1,num2str(q)-1,num2str(r)-1,num2str(s)-1,num2str(t)-1,num2str(u)-1,num2str(v)-1];
save(file);
end
end
end
end
end
end
end
end
end
end
Thank you very much!
4 件のコメント
Adam Danz
2020 年 6 月 15 日
Code from OP with indentation to see its structure.
%% Create the x vector for all possible combinations
for m=1:2
x(1)=m-1;
for n=1:2
x(2)=n-1;
for o=1:2
x(3)=o-1;
for p=1:2
x(4)=p-1;
for q=1:2
x(5)=q-1;
for r=1:2
x(6)=r-1;
for s=1:2
x(7)=s-1;
for t=1:2
x(8)=t-1;
for u=1:2
x(9)=u-1;
for v=1:2
x(10)=v-1;
%% Run the model
[A,B]=Zmultipleexecution(x);
%% Save the results in a .mat file
file=['C:\Users\XXX\Documents\',num2str(m)-1,num2str(n)-1,num2str(o)-1,num2str(p)-1,num2str(q)-1,num2str(r)-1,num2str(s)-1,num2str(t)-1,num2str(u)-1,num2str(v)-1];
save(file);
end
end
end
end
end
end
end
end
end
end
Adam Danz
2020 年 6 月 15 日
So, x is a 1x10 vector of 1s and 0s, is that correct?
Lenilein
2020 年 6 月 15 日
Adam Danz
2020 年 6 月 15 日
Then my answer should be what you're looking for.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および 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!