how to remove repeating rows from a matrix?(there is a case)
1 view (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Andrei Bobrov
on 13 Oct 2011
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)
More Answers (0)
See Also
Categories
Find more on Desktop in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!