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 Comments

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
So, x is a 1x10 vector of 1s and 0s, is that correct?
x is a 1 x i vector (i is stored in separate model parameter) of 1s and 0s
Then my answer should be what you're looking for.

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 15 Jun 2020
Edited: Adam Danz on 15 Jun 2020
It looks like input x is a 1x10 vector of 1s and 0s starting from [0 0 0 0 0 0 0 0 0 0] and ending at [1 1 1 1 1 1 1 1 1 1].
This loop produces the 1024 permutations of x in 'permMat' and then loops through each row.
% For 10 variables,
permMat = dec2bin(0:bin2dec('1111111111')); % 10 ones.
for i = 1:size(permMat,1)
inputs = str2double(num2cell(permMat(i,:))); % Convert to vector of doubles
[A,B]=Zmultipleexecution(inputs);
% Whever else you need to do.....
end
The permMat should produce the same sequence as your loop in the same order, too. Here are the first few rows/iterations. Each row is converted to a vector of doubles within the loop.
>> permMat
ans =
1024×10 char array
'0000000000' % Converted to [0 0 0 0 0 0 0 0 0 0] in loop.
'0000000001'
'0000000010'
'0000000011'
'0000000100'
'0000000101'
'0000000110'
'0000000111'
'0000001000'
{updated}
Alternatively, you can convert the char array to a matrix before the loop,
permMat = dec2bin(0:bin2dec('1111111111')); % 10 ones.
permMatDbl = str2double(num2cell(permMat));
for i = 1:size(permMat,1)
inputs = permMatDbl(i,:);
[A,B]=Zmultipleexecution(inputs);
% Whever else you need to do.....
end

4 Comments

Thanks, this is very useful!
Now remains the second part of my question: I would like to remove all elements of permMat for which more than one of the last three digits is equal to 1, any hint?
Within the for-loop, skip iterations that match that condition.
for i = 1:size(permMat,1)
inputs = str2double(num2cell(permMat(i,:))); % Convert to vector of doubles
if sum(inputs(end-2:end)) > 1
continue
else
[A,B]=Zmultipleexecution(x);
end
end
Alternatively you could convert permMat to a numeric matrix and eliminate rows before the loop.
This is the better method.
permMat = dec2bin(0:bin2dec('1111111111')); % 10 ones.
permMatDbl = str2double(num2cell(permMat));
removeIdx = sum(permMatDbl(:,end-2:end),2) > 1;
permMatDbl(removeIdx, :) = [];
for i = 1:size(permMatDbl,1)
inputs = permMatDbl(i,:)l
[A,B]=Zmultipleexecution(inputs);
% Whever else you need to do.....
end
Great, thank you so much!!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Asked:

on 15 Jun 2020

Commented:

on 15 Jun 2020

Community Treasure Hunt

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

Start Hunting!