Generate matrices of the form [a b c d]

조회 수: 1 (최근 30일)
laurie
laurie 2015년 3월 11일
댓글: Stephen23 2015년 3월 11일
If I have matrices of the form [a b c d]
where
2<=a<=4
1<=b<=2
3<=c<=4
2<=d<=4
Could someone help me with the code/pseudo code to generate all possible matrices?
  댓글 수: 3
laurie
laurie 2015년 3월 11일
yes they are whole numbers
Stephen23
Stephen23 2015년 3월 11일
You can easily generate a cell array containing the requested permutations:
X = cell(1,4);
[~,X{:}] = ndgrid(1,2:4,1:2,3:4,2:4);
X = num2cell(cat(1,X{:}),1);

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

채택된 답변

Image Analyst
Image Analyst 2015년 3월 11일
Of course you could just do a trivial, brute force way of 4 nested "for" loops, which I'm sure you can do. (If not, let us know.) But I'm thinking that you're wanting some more clever method, right? Or not right, and the trivial way is fine?
Oh well, here's the trivial way, just to save time:
for a = 2:4
for b = 1:2
for c = 3:4
for d = 2:4
out = [a,b,c,d]
end
end
end
end
  댓글 수: 1
laurie
laurie 2015년 3월 11일
I thought of this, but was wondering if there was another way...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by