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:26:20

Message: 1 of 4

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: ImageAnalyst

Date: 6 Oct, 2010 16:30:49

Message: 2 of 4

I can't do matrices that large on my system, but in general you can
try this

a=rand(1000,1000);
row1 = 500;
row2 = 900;
a(row1:row2, :) = [];

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

From: Sean

Date: 6 Oct, 2010 17:58:04

Message: 3 of 4

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <63ab1ed1-5eb0-423b-9138-c22acaba1946@i21g2000yqg.googlegroups.com>...
> I can't do matrices that large on my system, but in general you can
> try this
>
> a=rand(1000,1000);
> row1 = 500;
> row2 = 900;
> a(row1:row2, :) = [];

That's the way to delete, or you can just keep what you want:
%
a=rand(1000,1000);
row1 = 500;
row2 = 900;
a = a([1:row1-1, row2+1:end],:)

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

From: Sam T

Date: 7 Oct, 2010 13:43:04

Message: 4 of 4


Thank you guys. It definitely helped to bring down the time incurred in removing the rows.



"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8idbc$ah3$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <63ab1ed1-5eb0-423b-9138-c22acaba1946@i21g2000yqg.googlegroups.com>...
> > I can't do matrices that large on my system, but in general you can
> > try this
> >
> > a=rand(1000,1000);
> > row1 = 500;
> > row2 = 900;
> > a(row1:row2, :) = [];
>
> That's the way to delete, or you can just keep what you want:
> %
> a=rand(1000,1000);
> row1 = 500;
> row2 = 900;
> a = a([1:row1-1, row2+1:end],:)

Tags for 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