Thread Subject: How could I cut a matrix?

Subject: How could I cut a matrix?

From: QIUSI

Date: 9 Jun, 2011 20:45:08

Message: 1 of 4

How could I cut a matrix into several small groups according to the 1st column,
and I don't know how many subgroups I may get and the size of the subgroups are not the same(rows).


Say I have a matrix,

1 23 34
1 2 3
3 45 76
3 5 66
3 2 7
3 23 32
4 2 2
4 5 27
4 1 0
....

And finally, I would like to get :

1 23 34
1 2 3

and

3 45 76
3 5 66
3 2 7
3 23 32

and

4 2 2
4 5 27
4 1 0

and
...

How could I do that? Thanks in advance!

Subject: How could I cut a matrix?

From: Matthew

Date: 10 Jun, 2011 02:11:04

Message: 2 of 4

There may be a more elegant way, but I would call unique on the first column, which will give you an array representing the groups to divide the matrix into. From there you can segment it farther.

%%%%%%
%find unique groups
group_ids = unique(matrix(:,1));

groups = cell(1,numel(group_ids));

for cnt = 1:numel(group_ids)
   mask = matrix(:,1) == group_ids(cnt);
   groups{cnt} = matrix(mask,:);
end



"QIUSI " <em5357@hotmail.com> wrote in message <isrbck$gah$1@newscl01ah.mathworks.com>...
> How could I cut a matrix into several small groups according to the 1st column,
> and I don't know how many subgroups I may get and the size of the subgroups are not the same(rows).
>
>
> Say I have a matrix,
>
> 1 23 34
> 1 2 3
> 3 45 76
> 3 5 66
> 3 2 7
> 3 23 32
> 4 2 2
> 4 5 27
> 4 1 0
> ....
>
> And finally, I would like to get :
>
> 1 23 34
> 1 2 3
>
> and
>
> 3 45 76
> 3 5 66
> 3 2 7
> 3 23 32
>
> and
>
> 4 2 2
> 4 5 27
> 4 1 0
>
> and
> ...
>
> How could I do that? Thanks in advance!

Subject: How could I cut a matrix?

From: Sven

Date: 10 Jun, 2011 02:31:06

Message: 3 of 4

You can also do this sneakily with arrayfun(), given your matrix called "myMat".

myGroups = arrayfun(@(x)myMat(myMat(:,1)==x,:), unique(myMat(:,1)), 'UniformOutput',false)

Thanks,
Sven.

"QIUSI " <em5357@hotmail.com> wrote in message <isrbck$gah$1@newscl01ah.mathworks.com>...
> How could I cut a matrix into several small groups according to the 1st column,
> and I don't know how many subgroups I may get and the size of the subgroups are not the same(rows).
>
>
> Say I have a matrix,
>
> 1 23 34
> 1 2 3
> 3 45 76
> 3 5 66
> 3 2 7
> 3 23 32
> 4 2 2
> 4 5 27
> 4 1 0
> ....
>
> And finally, I would like to get :
>
> 1 23 34
> 1 2 3
>
> and
>
> 3 45 76
> 3 5 66
> 3 2 7
> 3 23 32
>
> and
>
> 4 2 2
> 4 5 27
> 4 1 0
>
> and
> ...
>
> How could I do that? Thanks in advance!

Subject: How could I cut a matrix?

From: rocwoods

Date: 10 Jun, 2011 02:49:05

Message: 4 of 4

You can also achieve your goal with accumarray function like this:

 a = [1 23 34
1 2 3
3 45 76
3 5 66
3 2 7
3 23 32
4 2 2
4 5 27
4 1 0];
A = accumarray(a(:,1),1:size(a,1),[],@(x){a(x,:)});
A(~cellfun(@isempty,A))

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
accumarray rocwoods 9 Jun, 2011 22:54:06
cut Zoe 9 Jun, 2011 16:49:10
segment Zoe 9 Jun, 2011 16:49:10
subgroup Zoe 9 Jun, 2011 16:49:10
group Zoe 9 Jun, 2011 16:49:10
rssFeed for this Thread

Contact us at files@mathworks.com