### Tags: asmaller, based, columns, colums, contain, delete, deleting, elements, index, matlab, matrix, programming, rows

# Deleting rows/colums based on an index matrix

On Programmer » Matlab

2,245 words with 3 Comments; publish: Sun, 27 Apr 2008 02:40:00 GMT; (20078.13, « »)

Hi All,

I'd like to delete either rows or columns from a matrix based on a

smaller matrix, the elements of which contain the rows/columns to be

deleted.

E.g

A data matrix 100 by 10

An index matrix containing [2 3]

I want to delete rows of my data matrix that correspond to the

elements in my index matrix, i.e. rows 2 and 3.

I'm sure there's a simple way to do this without resorting to nested

for loops.

Thanks for helping out a beginner in Matlab!

*http://matlab.todaysummary.com/q_matlab_12661.html*

All Comments

Leave a comment...

- 3 Comments
- data=magic(10) %make some data
trashrows=[2 3] %indices you want to get rid of

trashcols=[5 7]

rows=1:1:size(data,1)

cols=1:1:size(data,2)

keeprows=setdiff(rows,trashrows); %indices you want to keep

keepcols=setdiff(cols,trashcols);

gooddata=data(keeprows,keepcols); %the data you want

there might be a better way to do this, but this is probably

sufficient

#1; Sun, 27 Apr 2008 02:42:00 GMT

- data=magic(10) %make some data
- Something like this should do the trick.
a = rand(100,10);

b = [2 3];

a(b,:) = [];

Jeremy

"Jason Choo" <jason.choo.matlab.todaysummary.com.buseco.monash.edu.au> wrote in message

news:ef5a909.-1.matlab.todaysummary.com.webcrossing.raydaftYaTP...

> Hi All,

> I'd like to delete either rows or columns from a matrix based on a

> smaller matrix, the elements of which contain the rows/columns to be

> deleted.

> E.g

> A data matrix 100 by 10

> An index matrix containing [2 3]

> I want to delete rows of my data matrix that correspond to the

> elements in my index matrix, i.e. rows 2 and 3.

> I'm sure there's a simple way to do this without resorting to nested

> for loops.

> Thanks for helping out a beginner in Matlab!

#2; Sun, 27 Apr 2008 02:43:00 GMT

- Something like this should do the trick.
- Thanks a heap Jeremy and Matt, that works!
Regards,

Jason

Jeremy Barry wrote:

>

> Something like this should do the trick.

> a = rand(100,10);

> b = [2 3];

> a(b,:) = [];

> Jeremy

>

> "Jason Choo" <jason.choo.matlab.todaysummary.com.buseco.monash.edu.au> wrote in

message

> news:ef5a909.-1.matlab.todaysummary.com.webcrossing.raydaftYaTP...

on

> a

to

> be

> nested

>

>

#3; Sun, 27 Apr 2008 02:44:00 GMT

- Thanks a heap Jeremy and Matt, that works!