Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Christopher
Christopher le 21 Nov 2014
Commenté : Azzi Abdelmalek le 21 Nov 2014
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.

Réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 21 Nov 2014
Modifié(e) : Azzi Abdelmalek le 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 commentaires
Christopher
Christopher le 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
Azzi Abdelmalek
Azzi Abdelmalek le 21 Nov 2014
repmat(A(1,:)==5,size(A,1),1)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by