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

2 views (last 30 days)
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.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 21 Nov 2014
Edited: Azzi Abdelmalek on 21 Nov 2014
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 Comments
Christopher
Christopher on 21 Nov 2014
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

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!