Question

I’ve a matrix A = (4*4) and a cell array B {4,1}. I’d like to find all the values of B in A, searching row by row and after I’d like to delete the correspondent column associated to this particular value. I’ve a problem using bsxfun o cellfun and find function with a matrix and a cell array. I‘ve tried to convert the cell array into a matrix but I don’t have more the exact correspondence.

For example: A =

 1     5    10    23
 2     4     2    18
 3     3     5    14
 1     9    10     4

B =

 1 
 2     4
 3     3    14
 1

To obtain:

C =

 10
 2
 5
 10

Thanks in advance, L.

Was it helpful?

Solution

Here's how:

C = cellfun(@(x, y){sparse(1,find(ismember(x,y),numel(y)),true,1,size(A,2))}, ...
      mat2cell(A, ones(size(A, 1), 1), size(A, 2)), B(:));
C = A(:, all(~vertcat(C{:})));

The cellfun is fed with two cell arrays: the first one contains the rows of A and second one is B. The anonymous function is the tricky part; it operates on a pair of two corresponding rows as follows:

  1. It employs ismember to check which columns in A contain any of the elements in B.
  2. It uses find to pick only the first N ones, with respect to the number of elements in the B.
  3. It uses sparse as a fancy way of zeroing out the rest of the elements.

For your example it would look like this:

A = [1 5 10 23; 2 4 2 18; 3 3 5 14; 1 9 10 4];         
B = {1; [2 4]; [3 3 14]; 1};

C = cellfun(@(x, y){sparse(1,find(ismember(x,y),numel(y)),true,1,size(A,2))}, ...
    mat2cell(A, ones(size(A, 1), 1), size(A, 2)), B(:));

which yields:

C =
    {
        [1   0   0   0]
        [1   1   0   0]
        [1   1   0   1]
        [1   0   0   0]
    }

After that, it's a simple matter of logical indexing to pick the resulting columns:

C = A(:, all(~vertcat(C{:})));

which in this case should be:

C =  
   10
    2
    5
   10
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top