Thread Subject: Stumped on a Matrix Analysis

Subject: Stumped on a Matrix Analysis

From: gozer

Date: 25 Nov, 2009 12:12:03

Message: 1 of 3

I am a novice and stumped with what may be a simple problem but not sure how to attack. I have a matrix A=m x n and a smaller matrix filterA=[2:6,21,34,35:43]. I would like to return a matrix B that contains all rows of A whose value in col 'n' matches a value in filterA.
I have looked at 'filter', 'sort', sortrows, 'ismember' and others but none seem to be the right thing. Even tried multiple if-thens but that got messy quickly.

Subject: Stumped on a Matrix Analysis

From: Jos (10584)

Date: 25 Nov, 2009 12:56:19

Message: 2 of 3

"gozer " <wolfeb@timken.com> wrote in message <hej6uj$hqq$1@fred.mathworks.com>...
> I am a novice and stumped with what may be a simple problem but not sure how to attack. I have a matrix A=m x n and a smaller matrix filterA=[2:6,21,34,35:43]. I would like to return a matrix B that contains all rows of A whose value in col 'n' matches a value in filterA.
> I have looked at 'filter', 'sort', sortrows, 'ismember' and others but none seem to be the right thing. Even tried multiple if-thens but that got messy quickly.

From your description I think your after this (a small example might have help here!)

% data
  A = [ 1 2 3 ; 2 3 4 ; 1 3 4 ; 2 1 5 ; 2 4 3 ; 3 1 3] ; % arbitrary m-by-n array
  filterA = [3 4] ;

% task: keep only those rows in A for which the last element is present in "filterA"
% engine (can be written in a one-liner)
  Col_n_of_A = A(:,end) ; % n-th, i.e. last column of A
  tf = ismember(Col_n_of_A, filterA) ;
  B = A(tf,:)

hth
Jos

Subject: Stumped on a Matrix Analysis

From: gozer

Date: 26 Nov, 2009 01:22:05

Message: 3 of 3

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hej9hi$qko$1@fred.mathworks.com>...
> "gozer " <wolfeb@timken.com> wrote in message <hej6uj$hqq$1@fred.mathworks.com>...
> > I am a novice and stumped with what may be a simple problem but not sure how to attack. I have a matrix A=m x n and a smaller matrix filterA=[2:6,21,34,35:43]. I would like to return a matrix B that contains all rows of A whose value in col 'n' matches a value in filterA.
> > I have looked at 'filter', 'sort', sortrows, 'ismember' and others but none seem to be the right thing. Even tried multiple if-thens but that got messy quickly.
>
> From your description I think your after this (a small example might have help here!)
>
> % data
> A = [ 1 2 3 ; 2 3 4 ; 1 3 4 ; 2 1 5 ; 2 4 3 ; 3 1 3] ; % arbitrary m-by-n array
> filterA = [3 4] ;
>
> % task: keep only those rows in A for which the last element is present in "filterA"
> % engine (can be written in a one-liner)
> Col_n_of_A = A(:,end) ; % n-th, i.e. last column of A
> tf = ismember(Col_n_of_A, filterA) ;
> B = A(tf,:)
>
> hth
> Jos

Jos,
You nailed it dead on. Code does exactly what I was looking for. You saved me a bunch of stress.....owe you one!
thanks.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
matrices gozer 25 Nov, 2009 07:14:09
analysis gozer 25 Nov, 2009 07:14:09
filter gozer 25 Nov, 2009 07:14:09
rssFeed for this Thread

Contact us at files@mathworks.com