Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

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

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

From: Vinh Le

Date: 20 Mar, 2010 18:01:06

Message: 1 of 4

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.

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

From: Vinh Le

Date: 20 Mar, 2010 18:51:03

Message: 2 of 4

ops!!! matrixB is an array 1xn, not n by 1
matrixB = [1 2 2 1 2]

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

From: Sadik

Date: 20 Mar, 2010 18:52:03

Message: 3 of 4

Hello Vinh,

How about this one:

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

Best.

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

From: Vinh Le

Date: 20 Mar, 2010 18:59:07

Message: 4 of 4

"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 ^_^

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us