Create all combinations of a vector
Afficher commentaires plus anciens
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 commentaires
Adam Danz
le 15 Juin 2020
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
le 15 Juin 2020
So, x is a 1x10 vector of 1s and 0s, is that correct?
Lenilein
le 15 Juin 2020
Adam Danz
le 15 Juin 2020
Then my answer should be what you're looking for.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!