Create all combinations of a vector
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Lenilein
el 15 de Jun. de 2020
Comentada: Lenilein
el 15 de Jun. de 2020
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 comentarios
Respuesta aceptada
Adam Danz
el 15 de Jun. de 2020
Editada: Adam Danz
el 15 de Jun. de 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 comentarios
Adam Danz
el 15 de Jun. de 2020
Editada: Adam Danz
el 15 de Jun. de 2020
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
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!