Is there any ways to find all elements' rows and cols number at once?

1 view (last 30 days)
I'm keep finding if there's any way to find all elements' rows and cols number at once..
I need to put elements' column number to table.. (Dont need row number..)
There are many data, so i can't do it manually.
for example
A=[1,2,3;4,5,6]
I want to find each elements' column number
results like,
ans=1 2 3 1 2 3
(cause each elements' column number is 1 2 3 1 2 3 )

Accepted Answer

Walter Roberson
Walter Roberson on 26 Oct 2020
Edited: Walter Roberson on 26 Oct 2020
[~, column_numbers] = find(Array)
[~, column_numbers] = find(Array == value) %or any relationship
Note: in the specific case where there is exactly one matching column per row, or you only want the first or last match per row, there are some alternatives that do not involve find(). They can involve processing the array multiple times, but in a way that can be automatically vectorized.
Caution: if you are working on the case where there is exactly one match per row, then chances are that you would consider the result of the above find() to be out of order, as it will not be ordered by increasing row. To order by increasing row use
[column_numbers, ~] = find(Array .');
This has to do with the order that MATLAB stores arrays.

More Answers (0)

Categories

Find more on Graphics Object Programming 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!