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

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

Accepted Answer

Matt J
Matt J on 4 Aug 2022
Edited: Matt J on 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

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!