Search code examples
matlabmatlab-deployment

Generating the remaining values from a matrix using Matlab


Suppose there is a matrix generated using for loop which is given as

A = [ 1 3 4 ;...
      5 8 9 ;...
      6 3 8 ]

Let another matrix be generated from a given A matrix as

B = [ 5 3 8 ]

My question is: I want to generate a matrix by removing only elements of B from A column wise only.

output

C = [ 1 8 4 ;... 
      6 3 9 ]

How to do this using Matlab?


Solution

  • This should work assuming that the number of elements removed from each row is the same.

    A = [1 3 4; 5 8 9; 6 3 8];
    B = [3 5 8];
    
    C = A';
    C(find(A' == repmat(B, size(A, 2), 1))) = [];
    C = reshape(C, [], size(A, 1))'