# How to select certain columns of a matrix only when the first two rows always show 1 and the other two rows a 0?

2 ビュー (過去 30 日間)
Kim Arnold 2020 年 9 月 28 日
コメント済み: Kim Arnold 2020 年 10 月 1 日
Hi everybody!
I have a Matrix with 4 rows and 922 columns M(4,2300) containing ones and zeros as shown below
1 0 1 1 ...
1 0 1 1 ...
0 0 0 1 ...
0 0 0 0 ...
I want to keep only the columns when the first two rows show a 1 and row three and four show a zero.
In the example above column 1 and 3 fulfill the conditions whereas column 2 and 4 don't.
Does somebody have a nice idea to write a simple code to extract the columns to have an output for the example above like: [1 0 1 0]?
Thanks a lot for your help guys!!!

サインインしてコメントする。

### 採用された回答

Jalaj Gambhir 2020 年 10 月 1 日
Hi,
Use:
col = find(all(matrix(1:4,:)==[1;1;0;0]))
This will give you all the columns that satisfy this condition. You can then use
matrix(:,col)
to extract those columns from the matrix.
Hope this helps!
##### 1 件のコメント表示非表示 なし
Kim Arnold 2020 年 10 月 1 日
I did it with find(all(matrix(1:2,:)==1 & matrix(3:4,:)==0)); which is basically the same as you did :)

サインインしてコメントする。

### カテゴリ

Find more on Operators and Elementary Operations in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by