필터 지우기
필터 지우기

I need to create all a list of all possible states

조회 수: 3 (최근 30일)
V A
V A 2016년 4월 27일
댓글: Roger Stafford 2016년 4월 28일
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]

채택된 답변

Kevin
Kevin 2016년 4월 27일
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
V A
V A 2016년 4월 27일
This way works only for N<=30.
Error using repmat
Requested 2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2x2 (16.0GB) array exceeds
maximum array size preference. Creation of arrays greater than this limit may take a long time and
cause MATLAB to become unresponsive. See array size limit or preference panel for more information.
This error is super strange, because it doesn't take 16GB to create array I need....
Kevin
Kevin 2016년 4월 27일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Roger Stafford
Roger Stafford 2016년 4월 27일
A = zeros(2^N,N);
for k = 0:2^N-1
A(k+1,:) = 2*(dec2bin(k,N)-'0')-1;
end
  댓글 수: 2
V A
V A 2016년 4월 27일
This solution doesn't work for me for N>=26.
Error using zeros
Requested 67108864x26 (13.0GB) array exceeds maximum array size preference. Creation of arrays
greater than this limit may take a long time and cause MATLAB to become unresponsive. See array size
limit or preference panel for more information.
Subscripted assignment dimension mismatch.
Roger Stafford
Roger Stafford 2016년 4월 28일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by