フィルターのクリア

collect rows of a matrix having a desired number in the first row

1 回表示 (過去 30 日間)
Christopher
Christopher 2014 年 11 月 21 日
コメント済み: Azzi Abdelmalek 2014 年 11 月 21 日
If I have a matrix, what is the fastest way to collect the columns which satisfy a logical operation in the first row?
So if I have the matrix
0 5 3 6 4 5 8 4 7 5 1
5 6 7 5 6 7 8 9 4 5 2
3 4 3 5 8 3 5 4 4 5 1
9 6 7 1 6 3 7 4 5 3 8
9 2 9 5 0 4 2 5 6 3 4
if I wanted to collect all rows which have a 5 in the first row I would have
5 5 5
6 7 5
4 3 5
6 3 3
2 4 3
It is important that it is as fast as possible.

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 21 日
編集済み: Azzi Abdelmalek 2014 年 11 月 21 日
A=[0 5 3 6 4 5 8 4 7 5 1
5 6 7 5 6 7 8 9 4 5 2
3 4 3 5 8 3 5 4 4 5 1
9 6 7 1 6 3 7 4 5 3 8
9 2 9 5 0 4 2 5 6 3 4]
B=A(:,A(1,:)==5)
  2 件のコメント
Christopher
Christopher 2014 年 11 月 21 日
Sorry I made a mistake. How, instead, do I get the following index from the original matrix:
0 1 0 0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0
0 1 0 0 0 1 0 0 0 1 0
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 21 日
repmat(A(1,:)==5,size(A,1),1)

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

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by