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:
optimum way of deleting specified number of rows from matrix

Subject: optimum way of deleting specified number of rows from matrix

From: Sam T

Date: 6 Oct, 2010 16:28:37

Message: 1 of 5

Hi,

I have a matrix (lets say 10k x 10k) and I want to delete all rows starting from 'r1' and ending at 'r2'. What do you think is the best way to carry out this operation.

At this moment I use the "for" loop as follows, and I am sure that there exist better and more optimum ways of doing it. I realize that there are other threads in Matlab central which correspond to similar issue, but I was not able to get much out of them for this specific problem where I am deleting rows from r1 to r2.

My current approach:

for j= r2 :-1: r1
matrix(j,:)=[];
end

I run into this problem quite often, and the computing time becomes a serious issue when the difference b/w r1 and r2 i.e. (r2 - r1) is large, along with large matrices. I am looking for a better approach to solve this.

I will appreciate if other folks of this community can let me know their thoughts on this.

Thanks so much.

Subject: optimum way of deleting specified number of rows from matrix

From: Roger Stafford

Date: 6 Oct, 2010 17:53:20

Message: 2 of 5

"Sam T" <aero.sam.t@gmail.com> wrote in message <i8i83l$mfp$1@fred.mathworks.com>...
> Hi,
>
> I have a matrix (lets say 10k x 10k) and I want to delete all rows starting from 'r1' and ending at 'r2'. What do you think is the best way to carry out this operation.
>
> At this moment I use the "for" loop as follows, and I am sure that there exist better and more optimum ways of doing it. I realize that there are other threads in Matlab central which correspond to similar issue, but I was not able to get much out of them for this specific problem where I am deleting rows from r1 to r2.
>
> My current approach:
>
> for j= r2 :-1: r1
> matrix(j,:)=[];
> end
>
> I run into this problem quite often, and the computing time becomes a serious issue when the difference b/w r1 and r2 i.e. (r2 - r1) is large, along with large matrices. I am looking for a better approach to solve this.
>
> I will appreciate if other folks of this community can let me know their thoughts on this.
>
> Thanks so much.
- - - - - - - -
  You are right to run your for-loop backwards. However, doing this should be faster:

 M(r1:r2,:) = [];

Or else you could try this.

 M = M([1:r1-1,r2+1:end],:);

Roger Stafford

Subject: optimum way of deleting specified number of rows from matrix

From: James Tursa

Date: 6 Oct, 2010 18:07:05

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i8id2g$loi$1@fred.mathworks.com>...
>
> You are right to run your for-loop backwards. However, doing this should be faster:
>
> M(r1:r2,:) = [];
>
> Or else you could try this.
>
> M = M([1:r1-1,r2+1:end],:);

FYI, running the loop forwards or backwards doesn't make any difference ... MATLAB has to copy the entire data block (except the deleted rows) in both cases for each iteration. Doing it in one step as you have suggested is the best way because it only copies the desired data block once.

James Tursa

Subject: optimum way of deleting specified number of rows from matrix

From: Sean

Date: 6 Oct, 2010 18:15:15

Message: 4 of 5


> FYI, running the loop forwards or backwards doesn't make any difference ... MATLAB has to copy the entire data block (except the deleted rows) in both cases for each iteration. Doing it in one step as you have suggested is the best way because it only copies the desired data block once.
>
> James Tursa

James, it makes a difference because it'll skip every other row when doing it forward. E.g: it starts at row 500; then deletes row 500 and now 501 is 500. Then it deletes row 501 effectively leaving the original row 501.
For it to be done forward the line removed would have to be the same every time
for == 1:10 %inclusive difference between rows 1 and 2
  A(500,:) = [];
end

Subject: optimum way of deleting specified number of rows from matrix

From: James Tursa

Date: 6 Oct, 2010 18:43:22

Message: 5 of 5

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8iebj$h2o$1@fred.mathworks.com>...
>
> > FYI, running the loop forwards or backwards doesn't make any difference ... MATLAB has to copy the entire data block (except the deleted rows) in both cases for each iteration. Doing it in one step as you have suggested is the best way because it only copies the desired data block once.
> >
> > James Tursa
>
> James, it makes a difference because it'll skip every other row when doing it forward. E.g: it starts at row 500; then deletes row 500 and now 501 is 500. Then it deletes row 501 effectively leaving the original row 501.
> For it to be done forward the line removed would have to be the same every time
> for == 1:10 %inclusive difference between rows 1 and 2
> A(500,:) = [];
> end

I didn't mean to imply that simply reversing the indexing would give the same result. I was not clear on that in my previous post, and the indexing would have to be adjusted of course as you correctly point out. My point was that it doesn't matter in which order you delete the rows (assuming the loop indexing was proper) ... the data block copying has to happen in either case and that should be avoided in a loop.

James Tursa

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