Got Questions? Get Answers.
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:
Manipulate data

Subject: Manipulate data

From: Jonathan

Date: 11 Dec, 2008 00:09:02

Message: 1 of 7

I have some data in an n*m matrix (n is typically variable while m is allways the same number of columns) e.g.

A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]

what I would like to do is extract those data that have the same last row integer as the following file e.g.

B = [1 3 4] etc.,

and save it as a new variable i.e

c1 = [1 2 3 4;5 6 7 8;9 10 11 12];

plot the first two columns and then loop the same steps until end of file

c3 = [13 14 15 16;17 18 19 20];
c4 = [21 22 23 24]
reflect the other data sets that would eventually be extracted and then plotted as well etc.

Appreciate any help
Jon

Subject: Manipulate data

From: Jimy

Date: 11 Dec, 2008 03:03:02

Message: 2 of 7

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghplmu$7fu$1@fred.mathworks.com>...
> I have some data in an n*m matrix (n is typically variable while m is allways the same number of columns) e.g.
>
> A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
>
> what I would like to do is extract those data that have the same last row integer as the following file e.g.
>
> B = [1 3 4] etc.,
>
> and save it as a new variable i.e
>
> c1 = [1 2 3 4;5 6 7 8;9 10 11 12];
>
> plot the first two columns and then loop the same steps until end of file
>
> c3 = [13 14 15 16;17 18 19 20];
> c4 = [21 22 23 24]
> reflect the other data sets that would eventually be extracted and then plotted as well etc.
>
> Appreciate any help
> Jon


Hi Jon,

One possible way of doing this would be as follows:

A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4];
u=unique(A(:,end));
for i=1:length(u)
out=A(A(:,end)==u(i),1:end-1);
eval(['c' num2str(u(i)) '=out']);
end

I am sure there would be many other possible ways of implementing this same behavior.

-Jimy

Subject: Manipulate data

From: Jos

Date: 11 Dec, 2008 07:16:05

Message: 3 of 7

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghplmu$7fu$1@fred.mathworks.com>...
> I have some data in an n*m matrix (n is typically variable while m is allways the same number of columns) e.g.
>
> A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
>
> what I would like to do is extract those data that have the same last row integer as the following file e.g.
>
> B = [1 3 4] etc.,
>
> and save it as a new variable i.e
>
> c1 = [1 2 3 4;5 6 7 8;9 10 11 12];
>
> plot the first two columns and then loop the same steps until end of file
>
> c3 = [13 14 15 16;17 18 19 20];
> c4 = [21 22 23 24]
> reflect the other data sets that would eventually be extracted and then plotted as well etc.
>
> Appreciate any help
> Jon

First of all, avoid creating variables like c1, c2, c4. They clutter your workspace and do not convey any useful information that cannot be stored otherwise. And what if the unique values of the 5th column of A are non-integers, complex values ... Moreover, you need eval to do things like this, making your code both slow and less readable.
 
That being said, here is a solution using cell arrays:
% data
  A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
% engine
  Atemp = sortrows(A,5) ; % for ease of algorithm
  B = unique(Atemp(:,5)) ;
  n = histc(Atemp(:,5),B) ;
  C = mat2cell(Atemp(:,1:4),n,4) ;

Now, cell C{k} contains a matrix containing the first four columns of those rows of A that end in the value B(k).

hth
Jos

Subject: Manipulate data

From: Jonathan

Date: 11 Dec, 2008 17:25:04

Message: 4 of 7

Thanks Jos, this approach works great. I was struggling with a very cumbersome approach that involved an if statement which deleted those values that didn't match the ID and having the script do that multiple times.

So my other problem is how one goes about plotting say the first two columns in each array (i.e. C{k})? Since potentially I may have 100s of unique events, I would like to be able to plot each of these events concurrently in a figure.

Cheers
Jon

"Jos " <#10584@fileexchange.com> wrote in message <ghqenl$dvc$1@fred.mathworks.com>...
> "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghplmu$7fu$1@fred.mathworks.com>...
> > I have some data in an n*m matrix (n is typically variable while m is allways the same number of columns) e.g.
> >
> > A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
> >
> > what I would like to do is extract those data that have the same last row integer as the following file e.g.
> >
> > B = [1 3 4] etc.,
> >
> > and save it as a new variable i.e
> >
> > c1 = [1 2 3 4;5 6 7 8;9 10 11 12];
> >
> > plot the first two columns and then loop the same steps until end of file
> >
> > c3 = [13 14 15 16;17 18 19 20];
> > c4 = [21 22 23 24]
> > reflect the other data sets that would eventually be extracted and then plotted as well etc.
> >
> > Appreciate any help
> > Jon
>
> First of all, avoid creating variables like c1, c2, c4. They clutter your workspace and do not convey any useful information that cannot be stored otherwise. And what if the unique values of the 5th column of A are non-integers, complex values ... Moreover, you need eval to do things like this, making your code both slow and less readable.
>
> That being said, here is a solution using cell arrays:
> % data
> A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
> % engine
> Atemp = sortrows(A,5) ; % for ease of algorithm
> B = unique(Atemp(:,5)) ;
> n = histc(Atemp(:,5),B) ;
> C = mat2cell(Atemp(:,1:4),n,4) ;
>
> Now, cell C{k} contains a matrix containing the first four columns of those rows of A that end in the value B(k).
>
> hth
> Jos
>

Subject: Manipulate data

From: Jos

Date: 12 Dec, 2008 09:51:01

Message: 5 of 7

"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghridg$5b8$1@fred.mathworks.com>...
 
... fix top-posting
 
> "Jos " <#10584@fileexchange.com> wrote in message <ghqenl$dvc$1@fred.mathworks.com>...
> > "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghplmu$7fu$1@fred.mathworks.com>...
> > > I have some data in an n*m matrix (n is typically variable while m is allways the same number of columns) e.g.
> > >
> > > A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
> > >
> > > what I would like to do is extract those data that have the same last row integer as the following file e.g.
> > >
> > > B = [1 3 4] etc.,
> > >
> > > and save it as a new variable i.e
> > >
> > > c1 = [1 2 3 4;5 6 7 8;9 10 11 12];
> > >
> > > plot the first two columns and then loop the same steps until end of file
> > >
> > > c3 = [13 14 15 16;17 18 19 20];
> > > c4 = [21 22 23 24]
> > > reflect the other data sets that would eventually be extracted and then plotted as well etc.
> > >
> > > Appreciate any help
> > > Jon
> >
> > First of all, avoid creating variables like c1, c2, c4. They clutter your workspace and do not convey any useful information that cannot be stored otherwise. And what if the unique values of the 5th column of A are non-integers, complex values ... Moreover, you need eval to do things like this, making your code both slow and less readable.
> >
> > That being said, here is a solution using cell arrays:
> > % data
> > A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
> > % engine
> > Atemp = sortrows(A,5) ; % for ease of algorithm
> > B = unique(Atemp(:,5)) ;
> > n = histc(Atemp(:,5),B) ;
> > C = mat2cell(Atemp(:,1:4),n,4) ;
> >
> > Now, cell C{k} contains a matrix containing the first four columns of those rows of A that end in the value B(k).
> >
> > hth
> > Jos
> >

> Thanks Jos, this approach works great. I was struggling with a very cumbersome approach that involved an if statement which deleted those values that didn't match the ID and having the script do that multiple times.
>
> So my other problem is how one goes about plotting say the first two columns in each array (i.e. C{k})? Since potentially I may have 100s of unique events, I would like to be able to plot each of these events concurrently in a figure.
>
> Cheers
> Jon
>

I am not sure what you want to plot, but you can try something like this

for k=1:numel(C)
   temp = C{k} ; % extract data
   plot(temp(:,1),temp(:,2)) ;
   hold on ;
end
hold off

hth
Jos

Subject: Manipulate data

From: us

Date: 12 Dec, 2008 10:07:02

Message: 6 of 7

"Jonathan"
> how one goes about plotting say the first two columns in each array (i.e. C{k})? Since potentially I may have 100s of unique events, I would like to be able to plot each of these events concurrently in a figure...

one of the many solutions

% the data
     n=10;
     c=cell(n,1);
for i=1:n
     c{i}=i+rand(5,2);
end
% the engine
     lh=cellfun(@(x) line(x(:,1),x(:,2)),c);
% - set some props, eg, the color
     set(lh,{'color'},num2cell(jet(n),2));

us

Subject: Manipulate data

From: Jonathan

Date: 16 Dec, 2008 03:26:03

Message: 7 of 7

Jos,

Thanks very much. This works well.


"Jos " <#10584@fileexchange.com> wrote in message <ghtc65$1q$1@fred.mathworks.com>...
> "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghridg$5b8$1@fred.mathworks.com>...
>
> ... fix top-posting
>
> > "Jos " <#10584@fileexchange.com> wrote in message <ghqenl$dvc$1@fred.mathworks.com>...
> > > "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message <ghplmu$7fu$1@fred.mathworks.com>...
> > > > I have some data in an n*m matrix (n is typically variable while m is allways the same number of columns) e.g.
> > > >
> > > > A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
> > > >
> > > > what I would like to do is extract those data that have the same last row integer as the following file e.g.
> > > >
> > > > B = [1 3 4] etc.,
> > > >
> > > > and save it as a new variable i.e
> > > >
> > > > c1 = [1 2 3 4;5 6 7 8;9 10 11 12];
> > > >
> > > > plot the first two columns and then loop the same steps until end of file
> > > >
> > > > c3 = [13 14 15 16;17 18 19 20];
> > > > c4 = [21 22 23 24]
> > > > reflect the other data sets that would eventually be extracted and then plotted as well etc.
> > > >
> > > > Appreciate any help
> > > > Jon
> > >
> > > First of all, avoid creating variables like c1, c2, c4. They clutter your workspace and do not convey any useful information that cannot be stored otherwise. And what if the unique values of the 5th column of A are non-integers, complex values ... Moreover, you need eval to do things like this, making your code both slow and less readable.
> > >
> > > That being said, here is a solution using cell arrays:
> > > % data
> > > A = [1 2 3 4 1;5 6 7 8 1;9 10 11 12 1;13 14 15 16 3; 17 18 19 20 3; 21 22 23 24 4]
> > > % engine
> > > Atemp = sortrows(A,5) ; % for ease of algorithm
> > > B = unique(Atemp(:,5)) ;
> > > n = histc(Atemp(:,5),B) ;
> > > C = mat2cell(Atemp(:,1:4),n,4) ;
> > >
> > > Now, cell C{k} contains a matrix containing the first four columns of those rows of A that end in the value B(k).
> > >
> > > hth
> > > Jos
> > >
>
> > Thanks Jos, this approach works great. I was struggling with a very cumbersome approach that involved an if statement which deleted those values that didn't match the ID and having the script do that multiple times.
> >
> > So my other problem is how one goes about plotting say the first two columns in each array (i.e. C{k})? Since potentially I may have 100s of unique events, I would like to be able to plot each of these events concurrently in a figure.
> >
> > Cheers
> > Jon
> >
>
> I am not sure what you want to plot, but you can try something like this
>
> for k=1:numel(C)
> temp = C{k} ; % extract data
> plot(temp(:,1),temp(:,2)) ;
> hold on ;
> end
> hold off
>
> hth
> Jos

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