Combination calculations and matrix manipulation

조회 수: 1 (최근 30일)
mortain Antonio
mortain Antonio 2011년 5월 14일
Hello, I am working on a matrix (m x n) and I want to pick up different groups of m elements which have to be non dependent. Practically speaking I need (n-1)*m+2. For n = 4 and m = 3 I need 11 groups and an example of matrix is: [1 2 3 4, 5 6 7 8, 9 10 11 12] and the groups might be
(1 5 9),(1 5 10),(1 5 11),(1 5 12) 4
(1 6 9),(1 7 9),(1 8 9) 3
(2 5 9),(3 5 9),(4 5 9) 3
(2 6 9) 1
I have the matrix written, do you have any suggestion how to write this problem assuming that n and m are completely random and the first group has to be composed by the first column.
Thank you very much Antonio

채택된 답변

Matt Fig
Matt Fig 2011년 5월 14일
Here is how to get all of them using NPERMUTEK
A = [1 2 3 4;
5 6 7 8;
9 10 11 12];
[m,n] = size(A);
I = npermutek(1:n,m);
J = cumsum(ones(size(I)),2);
R = A(J+(I-1)*m) % Look at each row.
If you find that NPERMUTEK gives you too many samplings, you could try one of the lesser combinatorial samplings found in this file: COMBINATOR
  댓글 수: 1
mortain Antonio
mortain Antonio 2011년 5월 14일
Thanks for the fast answer, I am giving a fast look and I'll let you know!
Again thanks!

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

추가 답변 (3개)

mortain Antonio
mortain Antonio 2011년 5월 14일
FOr both of them I need to change the code since:
I tried with COMBINATOR, but it does not take into account that has to pick one variable from each different row, thought that's the result:
MySet = [1 2 3 4; 5 6 7 8; 9 10 11 12];
MySetperms = combinator(length(MySet),3,'c')
MySetperms = MySet(MySetperms)
MySetperms =
1 5 9
1 5 2
1 9 2
5 9 2
I tried with your suggestion:
n=4;
m=3;
A = [1 2 3 4;
5 6 7 8;
9 10 11 12];
[m,n] = size(A);
[m,n] = size(A);
I = npermutek(1:n,m);
J = cumsum(ones(size(I)),2);
R = A(J+(I-1)*m) % Look at each row.
Output
??? Maximum recursion limit of 500 reached. Use
set(0,'RecursionLimit',N)
to change the limit. Be aware that exceeding your
available stack space can
crash MATLAB and/or your computer.
Error in ==> npermutek
Do you have any particular suggestion to give? Antonio
  댓글 수: 1
Matt Fig
Matt Fig 2011년 5월 14일
Your first approach using COMBINATOR is NOT what I recommended. Use the output from COMBINATOR the same way I used NPERMUTEK in my first post.
Your second attempt is very odd, since NPERMUTEK is not a recursive algorithm. Did you make changes to the code before you got that error? Because when I copy and paste your code I get R = 64-by-3 where each row has one pick for each row of A, EXACTLY the same as Andrei's code below.

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


Andrei Bobrov
Andrei Bobrov 2011년 5월 14일
more so?
A = [1 2 3 4;
5 6 7 8;
9 10 11 12];
A1 = mat2cell(A,[1 1 1],size(A,2));
[J K I] = meshgrid(A1{[2 3 1]});
R = [I(:) J(:) K(:)];

mortain Antonio
mortain Antonio 2011년 5월 16일
Hello, this is the code that an IT officer at Uni made for me for doing the task I was referring to. However, it is a structure what you get at the end, which means that eventually you need to change it to a matrix (numbers). Thanks everybody for your help. Given a matrix [N,M] the system takes a number of groups equal to (M-1)*N+1.
m=[1 2 3 4; 5 6 7 8; 9 10 11 12]
%m=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]
[rows columns]=size(m);
group=zeros(rows,1);
used=zeros(size(m));
thegroups={};
numberofgroups=0;
for c=1:columns
group(1)=sub2ind(size(m),1,c);
for d=1:columns
group(2)=sub2ind(size(m),2,d);
if(rows>2)
for e=1:columns
group(3)=sub2ind(size(m),3,e);
if(rows>3)
for f=1:columns
group(4)=sub2ind(size(m),4,f);
if(rows>4)
group(5)=sub2ind(size(m),5,f);
if any(used(group)==0)
m(group)
used(group)=1;numberofgroups=numberofgroups+1; thegroups{numberofgroups}=m(group);
end
else
if any(used(group)==0)
m(group)
used(group)=1;numberofgroups=numberofgroups+1; thegroups{numberofgroups}=m(group);
end
end
end
else
if any(used(group)==0)
m(group)
used(group)=1;numberofgroups=numberofgroups+1; thegroups{numberofgroups}=m(group);
end
end
end
else
if any(used(group)==0)
m(group)
used(group)=1;numberofgroups=numberofgroups+1; thegroups{numberofgroups}=m(group);
end
end
end
end
numberofgroups
for i=1:numberofgroups
thegroups{i}
end
Antonio
p.s. if you have any suggestion or critic, please let me know!
Again thank you very much for your help
  댓글 수: 3
Oleg Komarov
Oleg Komarov 2011년 5월 16일
Goog example how to AVOID vectorization on MatLab (Matrix Laboratory) - LOLZ
mortain Antonio
mortain Antonio 2011년 5월 17일
Would be nice if you would like to share a better version of this software....
However what it does is getting the numbers of the first columns (different rows) unless it finds the last row and there it gets all the other numbers of the row.
Finshed the last row goes to the next to the last. It still gets all the first numbers of firs column and gets all the numbers after the first position of the next to the last row and ends the last row by getting the first element.
Finished this row keeps the loop. It gets the first position since the rows are sorted and the first position is higher numbers, supposed to give a faster convergence for the other operations.
example v=[1 2 3 4, 5 6 7 8, 9 10 11 12, 13 14 15 16]
(1 5 9 13) (1 5 9 14) (1 5 9 15) (1 5 9 16)
(1 5 10 13) (1 5 11 13) (1 5 12 13)
(1 6 10 13) (1 7 10 13) (1 8 10 13)
(2 5 10 13) (3 5 10 13) (4 5 10 13)
I hope it helps to develop better code in future, or if you want to put your hands on this same...
How to conver from structure to numbers the matrix?

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

카테고리

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