필터 지우기
필터 지우기

need help in creating matrix.....please

조회 수: 1 (최근 30일)
andy ganteng
andy ganteng 2011년 11월 12일
I wanna create matrix like this A(m,n), which n defined by user input. matrix A is consist of combination between number 1,2,3. then the form of this matrix if n=3: A=[1 1 1; 2 1 1; 1 2 1; 1 1 2; 2 2 1; 2 1 2; 1 2 2; 2 2 2; 3 1 1; 1 3 1; 1 1 3; 3 3 1; 3 1 3; 1 3 3; 3 2 2; 2 3 2; 2 2 3; 1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 2 1; 3 1 2; 3 3 3] please help me to create this matrix.
thanks in advance.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 11월 12일
a = 1:3;
n = 3;
k = perms(reshape(ones(n,1)*a,1,[]));
aout1=unique(k(:,1:n),'rows');
OR
a2 = cell(1,n);
[a2{:}] = ndgrid(a);
aout2 = cell2mat(cellfun(@(x)x(:),a2,'un',0));
  댓글 수: 3
Andrei Bobrov
Andrei Bobrov 2011년 11월 12일
corrected 1st variant
andy ganteng
andy ganteng 2011년 11월 12일
wait, i have n=20, then both of code above cannot work....please answer...thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by