how to avoid the loop to reduce the computation time of this code (one solution of my last question):
I hope to find the column vectors of A(1:3,:)
whose corresponding values in M(4,:)
are not part of one of the vectors of the cell X
(and obviously not equal to one of these vectors). I look for a fast solution if X
is very large.
M = [1007 1007 4044 1007 4044 1007 5002 5002 5002 622 622;
552 552 300 552 300 552 431 431 431 124 124;
2010 2010 1113 2010 1113 2010 1100 1100 1100 88 88;
7 12 25 15 12 30 2 10 55 32 12];
Here I take directly A
:
A = [1007 4044 5002 622;
552 300 431 124;
2010 1113 1100 88];
A
contains unique column vectors of M(1:3,:)
X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};
[~, ~, subs] = unique(M(1:3,:)','rows');
A4 = accumarray(subs(:),M(4,:).',[],@(x) {x});
%// getting a mask of which columns we want
idxC(length(A4)) = false;
for ii = 1:length(A4)
idxC(ii) = ~any(cellfun(@(x) all(ismember(A4{ii},x)), X));
end
Displaying the columns we want
out = A(:,idxC)
Results:
>> out
out =
1007 4044
552 300
2010 1113
the column vector [5002;431;1100]
was eliminated because [2;10;55]
is contained in X{2} = [2 10 55 9 17]
the column vector [622;124;88]
was eliminated because [32 12] = X{4}
Another example: with the same X
M = [1007 4044 1007 4044 1007 5002 5002 5002 622 622 1007 1007 1007;
552 300 552 300 552 431 431 431 124 124 552 11 11;
2010 1113 2010 1113 2010 1100 1100 1100 88 88 2010 20 20;
12 25 15 12 30 2 10 55 32 12 7 12 7];
X = {[2 5 68 44],[2 10 55 9 17],[1 55 6 7 8 9],[32 12]};
A = [1007 4044 5002 622 1077;
552 300 431 124 11;
2010 1113 1100 88 20];
Results: (with scmg answer)
I get if A
sorted according to the first row: (correct result)
out =
1007 1007 4044
11 552 300
20 2010 1113
if I do not sort the matrix A
, I get: (false result)
out =
4044 5002 622
300 431 124
1113 1100 88
the column vector A(:,4) = [622;124;88]
should be eliminated because [32 12] = X{4}
.
the column vector [5002;431;1100]
should be eliminated because [2;10;55]
is contained in X{2} = [2 10 55 9 17]