How find the row number for different elements in a column?

1 Ansicht (letzte 30 Tage)
Varghese
Varghese am 4 Aug. 2022
Bearbeitet: Matt J am 4 Aug. 2022
Suppose I set a condition where I choose only a certain element from each column. Now how will I get the row number corresponding to each element I have selected from each column. Note that the result should be an array with n elements, where n= number of columns.

Akzeptierte Antwort

Matt J
Matt J am 4 Aug. 2022
Bearbeitet: Matt J am 4 Aug. 2022
As an example, suppose you wanted to find the first occurrence of a 2 in each column. Then one way would be,
A=randi(10,10,5)
A = 10×5
6 8 9 6 2 9 9 2 5 10 4 7 2 5 10 5 5 9 3 1 8 1 8 7 7 6 3 4 1 2 5 2 7 10 10 2 3 8 1 9 3 9 8 9 7 1 2 3 7 4
[~,firstInstance]=max(A==2)
firstInstance = 1×5
8 7 2 1 1

Weitere Antworten (0)

Kategorien

Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by