필터 지우기
필터 지우기

I need to generate all 0/1 matrices

조회 수: 2 (최근 30일)
andreas
andreas 2013년 7월 2일
Hi, i would like to generate all different 5x5 triangle matrices which just have 0 and 1 as elements.
It's important that i really get all and not just a lot.
Is there a command for it? If not how can i programm it?
I appreciate your help a lot.

채택된 답변

Walter Roberson
Walter Roberson 2013년 7월 2일
M = dec2bin(0:2^15-1, 15);
Now each row of M is the content of one triangular matrix, but in linear form. You can pre-construct the linear indices of the triangular matrix you want (upper or lower), and go through assigning through those indices in turn.
Here is a constructor:
AllTriang = arrayfun( @(N) subsassgn(zeros(5,5), struct('type', '()', 'subs', {[1:5 7:10 13:15 19:20 25]}), dec2bin(S, 15)), 0:2^15-1, 'Uniform', 0);
  댓글 수: 1
Walter Roberson
Walter Roberson 2013년 7월 3일
AllTriang = arrayfun( @(N) subsasgn(zeros(5,5), struct('type', '()', 'subs', {{[1:5 7:10 13:15 19:20 25]}}), dec2bin(N, 15)-'0'), 0:2^15-1, 'Uniform', 0);

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

추가 답변 (2개)

Kevin
Kevin 2013년 7월 2일
편집: Kevin 2013년 7월 2일
%This will flip through the linear indexes of a matrix and turn each value into % a binary one
A = rand(5)
for p=1:end
if A(p)<=.5
A(p)= 0
else
A(p)=1
end
end
Edit: triu(A) and tril(A) return the upper and lower triangular portions of A, respectively. Tack that onto my previous post and you should be all set.

andreas
andreas 2013년 7월 2일
Thank you Walter. The first part is a really great tip. Unfortunately i am not able to get the 2nd part going. Do you have the whole file avaiable for it?
  댓글 수: 1
Jan
Jan 2013년 7월 3일
@andreas: Please post comments to an answer in the corresponding comment section and explain "not able to get going" with details: EDo you get an error message? If so, which one?

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by