remove multiple rows of matrix based the value and index of row of another matrix

  • Follow


I have 2 matrices with the same row, matrixA (n  by k) and matrixB (n by 1)

matrixA =

       1              2              3              4       
       5              6              7              8       
       9             10             11             12       
      13             14             15             16       
      17             18             19             20     

matrixB =

       1       
       2       
       2       
       1       
       2    

now I would like to remove row of matrix A, based the value of element in matrix B
For example remove all row of matrixA when value of element in matrixB is equal 2
matrixA = removeRow(matrixB)
matrixA =

       1              2              3              4       
      13             14             15             16       
Only row 1 and row 4 of matrixA remained.

   The code I wrote as a mathlab script:
==========================================
 for i=1:length(matrixB)
             if matrix(1,i) = 2
               index = index+1;
               removeRow(index) = i;
            end
 end

matrixA(removeRow,:)=[]
=========================================
It does not work
???  Index of element to remove exceeds matrix dimensions.
0
Reply Vinh 3/20/2010 6:01:06 PM

ops!!! matrixB is an array 1xn, not n by 1
matrixB = [1 2 2 1 2]
0
Reply Vinh 3/20/2010 6:51:03 PM


Hello Vinh,

How about this one:

matrixA(matrixB==2,:) = [];

Best.
0
Reply Sadik 3/20/2010 6:52:03 PM

"Sadik " <sadik.hava@gmail.com> wrote in message <ho35gj$g0m$1@fred.mathworks.com>...
> Hello Vinh,
> 
> How about this one:
> 
> matrixA(matrixB==2,:) = [];
> 
> Best.

this is a perfect simplest code - thank you very much ^_^ 
0
Reply Vinh 3/20/2010 6:59:07 PM

3 Replies
707 Views

(page loaded in 0.028 seconds)

Similiar Articles:













7/17/2012 1:12:10 AM


Reply: