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:
selecting data besed on contents

Subject: selecting data besed on contents

From: Dave

Date: 14 Feb, 2009 12:56:01

Message: 1 of 8

sorry, i am a complete beginner, but i need to be proficient quickly for my dissertation. i've spent all morning trying to do this and now fully admit i need HELP!

I have an array of about 17000x2. i need to separate the data by the 2nd column which is either 0 or >0. I would like to end up with 2 arrays of the same size as the first one but with data where column 2 had a non-zero value, else 0, and the other where column 2 had a zero value, else 0.

please forgive my simple question, but its not easy when you dont know how!

thx

Subject: selecting data besed on contents

From: Image Analyst

Date: 14 Feb, 2009 13:48:01

Message: 2 of 8

I've read this several times and can't visualize what you want. It sounds like your output matrix would look just like your input matrix. Can you supply a simple example, with, say, 6 rows instead of 17000?

Subject: selecting data besed on contents

From: Dave

Date: 14 Feb, 2009 14:12:01

Message: 3 of 8

"Image Analyst" <imageanalyst@mailinator.com> wrote in message <gn6i2h$4gc$1@fred.mathworks.com>...
> I've read this several times and can't visualize what you want. It sounds like your output matrix would look just like your input matrix. Can you supply a simple example, with, say, 6 rows instead of 17000?

thanks for your help:

for example, what i have looks a little like:
matrix 1
12 0
4 2
10 0
0 3
1 0.5

what i want looks like:
matrix 2 matrix 3
0 0 12 0
4 2 0 0
0 0 10 0
0 3 0 0
1 0.5 0 0

so matrix 2 has all values of matrix 1 when matrix 1 column 2 is non-zero.
and martix 3 has all the values of matrix 1 when matrix 1 column 2 is zero.

Subject: selecting data besed on contents

From: Dave

Date: 14 Feb, 2009 14:20:03

Message: 4 of 8

Oh dear! i guess you need to view it in original format to get the picture... sorry! :S


"Dave " <yogi_cave@yahoo.com> wrote in message <gn6jfh$5td$1@fred.mathworks.com>...
> "Image Analyst" <imageanalyst@mailinator.com> wrote in message <gn6i2h$4gc$1@fred.mathworks.com>...
> > I've read this several times and can't visualize what you want. It sounds like your output matrix would look just like your input matrix. Can you supply a simple example, with, say, 6 rows instead of 17000?
>
> thanks for your help:
>
> for example, what i have looks a little like:
> matrix 1
> 12 0
> 4 2
> 10 0
> 0 3
> 1 0.5
>
> what i want looks like:
> matrix 2 matrix 3
> 0 0 12 0
> 4 2 0 0
> 0 0 10 0
> 0 3 0 0
> 1 0.5 0 0
>
> so matrix 2 has all values of matrix 1 when matrix 1 column 2 is non-zero.
> and martix 3 has all the values of matrix 1 when matrix 1 column 2 is zero.

Subject: selecting data besed on contents

From: Roger Stafford

Date: 14 Feb, 2009 21:20:05

Message: 5 of 8

"Dave " <yogi_cave@yahoo.com> wrote in message <gn6jfh$5td$1@fred.mathworks.com>...
> ......
> for example, what i have looks a little like:
> matrix 1
> 12 0
> 4 2
> 10 0
> 0 3
> 1 0.5
>
> what i want looks like:
> matrix 2 matrix 3
> 0 0 12 0
> 4 2 0 0
> 0 0 10 0
> 0 3 0 0
> 1 0.5 0 0
>
> so matrix 2 has all values of matrix 1 when matrix 1 column 2 is non-zero.
> and martix 3 has all the values of matrix 1 when matrix 1 column 2 is zero.

  Suppose the original 17000 by 2 matrix is called M.

 t = (M(:,2)==0);
 M1 = zeros(size(M));
 M1(~t,:) = M(~t,:);
 M2 = zeros(size(M));
 M2(t,:) = M(t,:);

Roger Stafford

Subject: selecting data besed on contents

From: Dave

Date: 15 Feb, 2009 08:42:04

Message: 6 of 8

Thats great! Thanks a lot!

M1(~t,:)=M(~t,:)

So, this line assigns the values in M1 according to M when the value in t is 1. Will it do this over all columns of t? (I am just trying to understand how it works a bit better, just in case i have to do more complex things with it!)

Thanks again!

Subject: selecting data besed on contents

From: Roger Stafford

Date: 15 Feb, 2009 19:52:01

Message: 7 of 8

"Dave " <yogi_cave@yahoo.com> wrote in message <gn8kgs$p2b$1@fred.mathworks.com>...
> Thats great! Thanks a lot!
>
> M1(~t,:)=M(~t,:)
>
> So, this line assigns the values in M1 according to M when the value in t is 1. Will it do this over all columns of t? (I am just trying to understand how it works a bit better, just in case i have to do more complex things with it!)
>
> Thanks again!

  The array 't' is a logical column vector and in M(t,:) performs an action known as "logical indexing". It acts just as though you had written

 f = find(t);

which would find all the numerical indices where 't' is true (one) and then followed by

 M(f,:)

which refers to just those rows of M given by the values in 'f'. Anywhere you see a logical quantity placed as an index in an array, it acts in this manner bypassing the necessity of doing the 'find' operation.

  Your question puzzles me a bit. You say "Will it do this over all columns of t?", but as I defined it 't' has only one column. I am not sure what you mean. Placed in the first index position of M, M(t,:), it refers to the row numbers of M. If I had written M(:,t), then 't' would be referring to the column numbers of M. It has nothing to do with whether 't' itself is a column vector or a row vector.

Roger Stafford

Subject: selecting data besed on contents

From: Dave

Date: 16 Feb, 2009 20:26:02

Message: 8 of 8

Sorry, that was unclear. i meant to ask, will that then carry out that function for all of the columns in the array. but it obviously does, cos it worked for two... what i actually have is an array of about 100 columns and only want some of those columns, so i cut certain columns out to make a smaller array. but then within those columns i want to separate the by certain properties of certain columns, which I am able to do now, thanks!

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