How do I find the indices of nonzero elements for a matrix of logical vectors?
17 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
For example, I have the following matrix with logical vectors as rows:
A=[1 0 0 0; 0 0 1 0; 0 0 0 1]
I would like to get a vector with the indices of the first nonzero element for each row, such that the answer for matrix A should be:
v=[1;3;4]
I'd be grateful for any tip how to do this. Thank you
0 commentaires
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 5 Mar 2022
Try find():
A=[1 0 0 0; 0 0 1 0; 0 0 0 1]
[rows, columns] = find(A)
rows and columns are the (row, column) location of all the non-zero values in A.
11 commentaires
Image Analyst
le 8 Mar 2022
I'd ask Matt to post his answer as an official Answer. If he does, then you can Accept his answer and award him reputation points. If you learned anything from any of the other Answers in a post you can optionally "Vote" for them to award that poster reputation points also. You can only Accept one answer but you can vote for as many as you want.
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!