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:
extract rows

Subject: extract rows

From: Jonathan

Date: 20 Mar, 2009 23:57:01

Message: 1 of 5

I have the following script...

j = [time data]; %8760*4 matrix
m = maxtab(:,1); %Column of data
rows = find(ismember(time, m,'rows')); %find those rows where m value matches time value
j(rows,:)=[];

What I am trying to do is extract those lines that correspond to the m value from matrix j. The last line deletes the rows in J that correspond to m. However, what I need is the opposite, which is delete all other values and retain only those J rows that correspond to the m values.

Thanks for your help,
Jon

Subject: extract rows

From: us

Date: 21 Mar, 2009 00:09:01

Message: 2 of 5

"Jonathan"
> j = [time data]; %8760*4 matrix
> m = maxtab(:,1); %Column of data
> rows = find(ismember(time, m,'rows')); %find those rows where m value matches time value
> j(rows,:)=[];
> What I am trying to do is extract those lines that correspond to the m value from matrix j. The last line deletes the rows in J that correspond to m. However, what I need is the opposite, which is delete all other values and retain only those J rows that correspond to the m values...

a nice example of the strength of
- logical indexing, which is what you want
and
- linear indexing, which is what FIND unnecessarily does for you in this case

one of the solutions

     d=[1,3,4,5,7,9];
     t=[1,7,2];
     ix=ismember(d,t)
% ix = 1 0 0 0 1 0
     dok=d(ix)
% dok = 1 7
     dnotok=d(~ix)
% dnotok = 3 4 5 9

us

Subject: extract rows

From: Jonathan

Date: 23 Mar, 2009 15:27:01

Message: 3 of 5

"us " <us@neurol.unizh.ch> wrote in message <gq1b6t$bf3$1@fred.mathworks.com>...
> "Jonathan"
> > j = [time data]; %8760*4 matrix
> > m = maxtab(:,1); %Column of data
> > rows = find(ismember(time, m,'rows')); %find those rows where m value matches time value
> > j(rows,:)=[];
> > What I am trying to do is extract those lines that correspond to the m value from matrix j. The last line deletes the rows in J that correspond to m. However, what I need is the opposite, which is delete all other values and retain only those J rows that correspond to the m values...
>
> a nice example of the strength of
> - logical indexing, which is what you want
> and
> - linear indexing, which is what FIND unnecessarily does for you in this case
>
> one of the solutions
>
> d=[1,3,4,5,7,9];
> t=[1,7,2];
> ix=ismember(d,t)
> % ix = 1 0 0 0 1 0
> dok=d(ix)
> % dok = 1 7
> dnotok=d(~ix)
> % dnotok = 3 4 5 9
>
> us

Hmmm, while this approach seems better than using find, it still does not resolve my original problem.

If my original matrix 'd' in your example above is 8760*4, and 't' is 9*1, what I would like to be able to do is for the 9 't'-values, use these to identify the same row values in 'd' and place those specific 'd'-rows in a new matrix say 'rows', which would have a size of 9*4.

Thanks again for your help.

Jon

Subject: extract rows

From: Roger Stafford

Date: 23 Mar, 2009 17:03:01

Message: 4 of 5

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <gq89o5$d6n$1@fred.mathworks.com>...
> Hmmm, while this approach seems better than using find, it still does not resolve my original problem.
>
> If my original matrix 'd' in your example above is 8760*4, and 't' is 9*1, what I would like to be able to do is for the 9 't'-values, use these to identify the same row values in 'd' and place those specific 'd'-rows in a new matrix say 'rows', which would have a size of 9*4.
>
> Thanks again for your help.
>
> Jon

  Urs has shown you the basic technique, Jonathan. Just apply his method to your particular problem. Don't use the 'rows' version of 'ismember' since you are only doing matching with the first column of j.

 p = ismember(j(:,1),m);
 j = j(p,:);

Roger Stafford

Subject: extract rows

From: Jonathan

Date: 23 Mar, 2009 17:15:03

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gq8fc5$icq$1@fred.mathworks.com>...
> "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <gq89o5$d6n$1@fred.mathworks.com>...
> > Hmmm, while this approach seems better than using find, it still does not resolve my original problem.
> >
> > If my original matrix 'd' in your example above is 8760*4, and 't' is 9*1, what I would like to be able to do is for the 9 't'-values, use these to identify the same row values in 'd' and place those specific 'd'-rows in a new matrix say 'rows', which would have a size of 9*4.
> >
> > Thanks again for your help.
> >
> > Jon
>
> Urs has shown you the basic technique, Jonathan. Just apply his method to your particular problem. Don't use the 'rows' version of 'ismember' since you are only doing matching with the first column of j.
>
> p = ismember(j(:,1),m);
> j = j(p,:);
>
> Roger Stafford

Thanks Urs and Roger. This was extremely helpful.

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