Effacer les filtres
Effacer les filtres

Simple Matrix Reduction Question. Ones and Zeros.

2 vues (au cours des 30 derniers jours)
Chad
Chad le 1 Avr 2020
I have a NxM matrix full of ones and zeros. Each row (1:N) has exactly one, one, somewhere along of the columns. I want to make a matrix Nx1 which shows which columns the ones appeared in. Example:
A = [ 0 0 0 1; 1 0 0 0; 0 1 0 0; 0 0 1 0]
B = [ 4 ; 1 ; 2 ; 3 ]
Should be very simple?

Réponse acceptée

Birdman
Birdman le 1 Avr 2020
Modifié(e) : Birdman le 1 Avr 2020
[rows,cols]=find(A==1);
[C,I]=sort(rows);
B=cols(I)

Plus de réponses (1)

Stephen23
Stephen23 le 1 Avr 2020
Simpler:
>> [B,~] = find(A.')
B =
4
1
2
3

Catégories

En savoir plus sur Matrices and Arrays 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!

Translated by