how to remove repeating rows from a matrix?(there is a case)

조회 수: 1 (최근 30일)
Zhang
Zhang 2011년 10월 13일
I want to combine all the possible no-repeating permutations of two or more 0,1 row vectors, for example, there are a=[1,0], b=[1,1,0], the combination of all their possible no-repeating permutations will be
0 1 0 1 1
0 1 1 0 1
0 1 1 1 0
1 0 0 1 1
1 0 1 0 1
1 0 1 1 0
I use the following script to ahieve this,
a=[1,0];
b=[1,1,0];
A=perms(a); %all the possible permutations, may contain repeating rows
B=perms(b);
[r_a,c_a]=size(A);
[r_b,c_b]=size(B);
AB=[];
for i=1:r_a
for j=1:r_b
COM1=[A(i,:) B(j,:)]; %combine ith rows in A with all rows in B
D1(j,1:c_a+c_b)=COM1;
end
AB=cat(1,AB,D1); %update AB on every ith loop
end
AB=unique(AB,'rows'); %delete repeating rows
% AB=union(AB,AB,'rows'); %union can also be used to delete repeating rows
but,in my real case, there are
a=[1,1,0,0,0];
b=[1,1,1,1,0,0];
c=[1,1,1,1,0,0,0];
d=[1,1,1,0,0];
e=[1,1,0,0];
I want to combine all the the possible no-repeating permutations(the result will be ABCDE), using method like the above script,
A=perms(a);
B=perms(b);
C=perms(c);
D=perms(d);
E=perms(e);
and then,
Step 1, combine A and B, delete repeating rows, and result in AB;
Step 2, combine AB and C, delete repeating rows, and result in ABC;
Step 3, combine ABC and D, delete repeating rows, and result in ABCD;
Step 4, combine ABCD and E, delete repeating rows, and result in ABCDE;
but using my method(either using "union" or "unique" to delete), when it comes to Step 4, it is out of memory,
Elapsed time is 1.452821 seconds.%Step 1 Elapsed time is 19.556511 seconds.%Step 2 Elapsed time is 456.368700 seconds.%Step 3 ??? Out of memory. Type HELP MEMORY for your options.
Error in ==> unique at 201 d = b(1:rows-1,:)~=b(2:rows,:);
Error in ==> combine_perms_5 at 85 ABCDE=unique(ABCDE,'rows');%here, I use unique
my questions are,
Q1 Are there simple ways to combine all the possible no-repeating permutations of a,b,c,d,e?
Q2 Are there simple ways to delete repeating rows, making sure that it is not out of memory?
I really appreciate your comment on this!

채택된 답변

Andrei Bobrov
Andrei Bobrov 2011년 10월 13일
A = perms([0 0 1 1 1]);
AB=unique(A(sum(A(:,1:2),2)==1 | sum(A(:,3:end),2)==2,:),'rows');
variant
a=[1,1,0,0,0];
b=[1,1,1,1,0,0];
c=[1,1,1,1,0,0,0];
d=[1,1,1,0,0];
e=[1,1,0,0];
Din = {a b c d e};
d = cellfun(@(x)unique(perms(x),'rows'),Din,'un',0);
ns = cellfun('size',d,1);
inarg = arrayfun(@(i1)1:i1,ns,'un',0);
M = cell(1,numel(Din));
[M{:}] = ndgrid(inarg{:});
outc = cellfun(@(x,y)x(y(:),:),d,M,'un',0);
out = [outc{:}];
solution on my computer (HP Compaq dx2400 Microtower IntelC2D 2.13GHz, RAM 2GB)
  댓글 수: 1
Zhang
Zhang 2011년 10월 13일
It works, thx indeed! another question, I run the script and the result variable "out" in workspace is an 315000*27 double variable, but "Min"and "Max" shows"Too many elements", I run max(out) and min(out), it shows,
>> max(out)
ans =
Columns 1 through 11
1 1 1 1 1 1 1 1 1 1 1
Columns 12 through 22
1 1 1 1 1 1 1 1 1 1 1
Columns 23 through 27
1 1 1 1 1
>> min(out)
ans =
Columns 1 through 11
0 0 0 0 0 0 0 0 0 0 0
Columns 12 through 22
0 0 0 0 0 0 0 0 0 0 0
Columns 23 through 27
0 0 0 0 0
how can it be all 1 or all 0 in "out"?

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by