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

3 次查看(过去 30 天)
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by