I need to create all a list of all possible states
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
V A
el 27 de Abr. de 2016
Comentada: Roger Stafford
el 28 de Abr. de 2016
I have discrete variables a_i=[1 -1]. I need to create a list of all states in system of N variables(# of states = 2^N). Or in other words I have a vector of length N, each component can have two values. How can I generate a list of all possible vectors?
For instance, for 3 variables I need to have
[1 1 1]
[1 1 -1]
[1 -1 1]
[-1 1 1]
[1 -1 -1]
[-1 1 -1]
[-1 -1 1]
[-1 -1 -1]
0 comentarios
Respuesta aceptada
Kevin
el 27 de Abr. de 2016
I have written my own function to do what you are asking for. The trick is to use the MATLAB function ndgrid.
>> A = allcombs({[1 -1], [1 -1], [1 -1]})
A =
1 1 1
1 1 -1
1 -1 1
1 -1 -1
-1 1 1
-1 1 -1
-1 -1 1
-1 -1 -1
Here is the definition of my function allcombs.m:
function A = allcombs(experimentOutcomes)
experimentOutcomes = flipud(experimentOutcomes(:));
c = cell(1, numel(experimentOutcomes));
[c{:}] = ndgrid(experimentOutcomes{:});
c = fliplr(c);
A = cell2mat(cellfun(@(v)v(:), c, 'UniformOutput',false));
end
4 comentarios
Kevin
el 27 de Abr. de 2016
But if N = 30, then
>> 2^30
ans =
1.0737e+09
So the matrix (that contains all possible combinations) will have 1 billion rows and 30 columns. Each matrix element takes 8 bytes. So total number of bytes ~= 240 GB.
Right?
Más respuestas (1)
Roger Stafford
el 27 de Abr. de 2016
A = zeros(2^N,N);
for k = 0:2^N-1
A(k+1,:) = 2*(dec2bin(k,N)-'0')-1;
end
2 comentarios
Roger Stafford
el 28 de Abr. de 2016
If that is the limit on your array sizes, then no 'double' solution for N>=26 could work. The matrix you request as an output is that size. You could probably get up to N = 29 by using the 'uint8' format, but that would be as far as you could go.
Ver también
Categorías
Más información sobre Logical en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!