[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Easy way to split a matrix according to column value
From: |
Przemek Klosowski |
Subject: |
Re: Easy way to split a matrix according to column value |
Date: |
Wed, 02 Feb 2011 10:05:43 -0500 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b2 Thunderbird/3.1.7 |
On 02/02/2011 09:49 AM, alewar wrote:
Hello,
perhaps someone can help me with this.
Suppose I have the following matrix:
m =
0 0 1
0 1 0
0 1 1
1 0 0
1 1 0
1 1 1
and I want to split it depending on the values of a column.
..
> Is there an easy way to do this without a for-loop?
The find() function returns a list of indices, which you can then use to
index the original array. Specifically, m(find(m(:,2)==1),:) and
m(find(m(:,3)==0),:) return your desired results:
For example, give me the submatrix for which the second column is 1
0 1 0
0 1 1
1 1 0
1 1 1
or for which the 3rd column is 0
0 1 0
1 0 0
1 1 0