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:
separate the rows in a matrix

Subject: separate the rows in a matrix

From: wahyoe Unggul

Date: 7 Apr, 2011 18:10:08

Message: 1 of 5

how to separate the rows in a matrix with the same element value, so that the three matrix
examples
A =[2 1 1 1;
      3 0 1 0;
      5 1 1 0;
      7 0 1 0;
      12 1 1 0]

results
B =[2 1 1]

C= [3 0 1 0;
      7 0 1 0]

D =[5 1 1 0;
     12 1 1 0]

thanks

Subject: separate the rows in a matrix

From: Miroslav Balda

Date: 7 Apr, 2011 18:56:04

Message: 2 of 5

"wahyoe Unggul" <wahyoe_slipnot@yahoo.co.id> wrote in message <inkum0$p4k$1@fred.mathworks.com>...
> how to separate the rows in a matrix with the same element value, so that the three matrix
> examples
> A =[2 1 1 1;
> 3 0 1 0;
> 5 1 1 0;
> 7 0 1 0;
> 12 1 1 0]
>
> results
> B =[2 1 1]
>
> C= [3 0 1 0;
> 7 0 1 0]
>
> D =[5 1 1 0;
> 12 1 1 0]
>
> thanks

Hi,

It is rather easy:

B = A(1,1:3);
C = A([2,4],:);
D = A([3,5],:);

Mira

Subject: separate the rows in a matrix

From: wahyoe Unggul

Date: 7 Apr, 2011 19:16:21

Message: 3 of 5

>
> B = A(1,1:3);
> C = A([2,4],:);
> D = A([3,5],:);
>
> Mira
hi mira
but what if the number of columns a lot, how to define the same line "(1 1 1)", "(0 1 0)" and "(1 1 0)"

Subject: separate the rows in a matrix

From: Matt J

Date: 7 Apr, 2011 19:57:04

Message: 4 of 5

"wahyoe Unggul" <wahyoe_slipnot@yahoo.co.id> wrote in message <inkum0$p4k$1@fred.mathworks.com>...
> how to separate the rows in a matrix with the same element value, so that the three matrix
===================

Is this what you want?

[~,i,j]=unique(A(:,2:end),'rows');

N=length(i);
S=cell(1,N);

for ii=1:N

    S{ii}=A(j==ii,:);
    
end

[C,D,B]=deal(S{:})

Subject: separate the rows in a matrix

From: wahyoe Unggul

Date: 7 Apr, 2011 20:06:04

Message: 5 of 5


> Is this what you want?
>
> [~,i,j]=unique(A(:,2:end),'rows');
>
> N=length(i);
> S=cell(1,N);
>
> for ii=1:N
>
> S{ii}=A(j==ii,:);
>
> end
>
> [C,D,B]=deal(S{:})

yes matt, Thank you very much

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