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:
Grouping similar elements in a matrix

Subject: Grouping similar elements in a matrix

From: Kara Zabetakis

Date: 7 Oct, 2010 19:53:04

Message: 1 of 8

So, I have a matrix with a lot of data that I want to split up into smaller matrices.

The starting matrix I have is something like:
[3 1 2 3
4 1 5 6
3 2 4 2
4 4 6 7
8.5 3 2 1]

And I want to split this up into separate matrices based on the number in the first column... so the final matrices would be
[3 1 2 3
3 2 4 2]

[4 1 5 6
4 4 6 7]

and

[8.5 3 2 1]

The problem is.. there is a lot of data and a lot of different numbers in the first column. So I need a way to separate the matrix that does not require me to know what the actual numbers will be in the first column. (hope this makes sense)

Thanks,

-Kara

Subject: Grouping similar elements in a matrix

From: Sean

Date: 7 Oct, 2010 20:08:04

Message: 2 of 8

"Kara Zabetakis" <kmz3189@gmail.com> wrote in message <i8l8ev$4hr$1@fred.mathworks.com>...
> So, I have a matrix with a lot of data that I want to split up into smaller matrices.
>
> The starting matrix I have is something like:
> [3 1 2 3
> 4 1 5 6
> 3 2 4 2
> 4 4 6 7
> 8.5 3 2 1]
>
> And I want to split this up into separate matrices based on the number in the first column... so the final matrices would be
> [3 1 2 3
> 3 2 4 2]
>
> [4 1 5 6
> 4 4 6 7]
>
> and
>
> [8.5 3 2 1]
>
> The problem is.. there is a lot of data and a lot of different numbers in the first column. So I need a way to separate the matrix that does not require me to know what the actual numbers will be in the first column. (hope this makes sense)
>
> Thanks,
>
> -Kara

I highly doubt you actually want create that many matrices. You'll then end up with a ton of matrices, you'll have to name them all according to something. It's generally a bad idea. In MATLAB the way to get around this is to use cell arrays. So here each cell of the cell array will be on of your matrices. You can then traverse the cell and do math on it easily with a for-loop or cellfun().

Here's the code to do what you want:
A = ...
[3 1 2 3
4 1 5 6
3 2 4 2
4 4 6 7
8.5 3 2 1];
A = sortrows(A,1);
idx = cumsum(diff([0;A(:,1)])>0);
idxn = histc(idx,unique(idx));
C = mat2cell(A,idxn,size(A,2))

%C is now a cell array with each cell being one of your matrices

Subject: Grouping similar elements in a matrix

From: someone

Date: 7 Oct, 2010 20:12:03

Message: 3 of 8

"Kara Zabetakis" <kmz3189@gmail.com> wrote in message <i8l8ev$4hr$1@fred.mathworks.com>...
> So, I have a matrix with a lot of data that I want to split up into smaller matrices.
>
> The starting matrix I have is something like:
> [3 1 2 3
> 4 1 5 6
> 3 2 4 2
> 4 4 6 7
> 8.5 3 2 1]
>
> And I want to split this up into separate matrices based on the number in the first column... so the final matrices would be
> [3 1 2 3
> 3 2 4 2]
>
> [4 1 5 6
> 4 4 6 7]
>
> and
>
> [8.5 3 2 1]
>
> The problem is.. there is a lot of data and a lot of different numbers in the first column. So I need a way to separate the matrix that does not require me to know what the actual numbers will be in the first column. (hope this makes sense)
>
> Thanks,
>
> -Kara

a = [3 1 2 3;
4 1 5 6;
3 2 4 2;
4 4 6 7;
8.5 3 2 1]

b = sort(a);
for ii = 1:length(b(:,1))
   c{ii} = b(ii,:); % note curly brackets - c is a cell array
end

% display results as c{1}, c{2}, ...

Subject: Grouping similar elements in a matrix

From: Sean

Date: 7 Oct, 2010 20:19:03

Message: 4 of 8


> b = sort(a);
> for ii = 1:length(b(:,1))
> c{ii} = b(ii,:); % note curly brackets - c is a cell array
> end

someone,
This does not preserve the original matrices since each column is sorted.

Subject: Grouping similar elements in a matrix

From: someone

Date: 7 Oct, 2010 20:35:05

Message: 5 of 8

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i8l9vn$fqg$1@fred.mathworks.com>...
>
> > b = sort(a);
> > for ii = 1:length(b(:,1))
> > c{ii} = b(ii,:); % note curly brackets - c is a cell array
> > end
>
> someone,
> This does not preserve the original matrices since each column is sorted.

OOPS... You're correct Sean.

% Simply replace:
sort(a)
% with
sortrows(a,1)

Of course you also supplied a "vectorized" version.

Subject: Grouping similar elements in a matrix

From: Branko

Date: 8 Oct, 2010 07:01:05

Message: 6 of 8

"Kara Zabetakis" <kmz3189@gmail.com> wrote in message <i8l8ev$4hr$1@fred.mathworks.com>...
> So, I have a matrix with a lot of data that I want to split up into smaller matrices.
>
> The starting matrix I have is something like:
> [3 1 2 3
> 4 1 5 6
> 3 2 4 2
> 4 4 6 7
> 8.5 3 2 1]
>
> And I want to split this up into separate matrices based on the number in the first column... so the final matrices would be
> [3 1 2 3
> 3 2 4 2]
>
> [4 1 5 6
> 4 4 6 7]
>
> and
>
> [8.5 3 2 1]
>
> The problem is.. there is a lot of data and a lot of different numbers in the first column. So I need a way to separate the matrix that does not require me to know what the actual numbers will be in the first column. (hope this makes sense)
>
> Thanks,
>
> -Kara

Another approach using unique:

A=[3 1 2 3
4 1 5 6
3 2 4 2
4 4 6 7
8.5 3 2 1];
[~,~,ix]=unique(A(:,1));
for i=1:max(ix)
     B{i}=A(ix==i,:);
end

Branko

Subject: Grouping similar elements in a matrix

From: Kara

Date: 9 Oct, 2010 23:14:04

Message: 7 of 8

Thanks everyone! Your responses were a lot of help.

Subject: Grouping similar elements in a matrix

From: Mohammad

Date: 27 Apr, 2014 18:40:12

Message: 8 of 8

"someone" wrote in message <i8l9ij$ia1$1@fred.mathworks.com>...
> "Kara Zabetakis" <kmz3189@gmail.com> wrote in message <i8l8ev$4hr$1@fred.mathworks.com>...
> > So, I have a matrix with a lot of data that I want to split up into smaller matrices.
> >
> > The starting matrix I have is something like:
> > [3 1 2 3
> > 4 1 5 6
> > 3 2 4 2
> > 4 4 6 7
> > 8.5 3 2 1]
> >
> > And I want to split this up into separate matrices based on the number in the first column... so the final matrices would be
> > [3 1 2 3
> > 3 2 4 2]
> >
> > [4 1 5 6
> > 4 4 6 7]
> >
> > and
> >
> > [8.5 3 2 1]
> >
> > The problem is.. there is a lot of data and a lot of different numbers in the first column. So I need a way to separate the matrix that does not require me to know what the actual numbers will be in the first column. (hope this makes sense)
> >
> > Thanks,
> >
> > -Kara
>
> a = [3 1 2 3;
> 4 1 5 6;
> 3 2 4 2;
> 4 4 6 7;
> 8.5 3 2 1]
>
> b = sort(a);
> for ii = 1:length(b(:,1))
> c{ii} = b(ii,:); % note curly brackets - c is a cell array
> end
>
> % display results as c{1}, c{2}, ...

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