How could I recreate the find function

1 Ansicht (letzte 30 Tage)
James Abrams
James Abrams am 30 Okt. 2016
Beantwortet: Star Strider am 30 Okt. 2016
I need to find values in a matrix that equal a given value (10), but I need to know their index/position in the matrix (won't use find function).
so if the matrix a = [2 10 10 5 7 4 10] I want to run my function and get ans = [2 3 7] to find where the 10's are in the first function

Antworten (1)

Star Strider
Star Strider am 30 Okt. 2016
One approach:
a = [2 10 10 5 7 4 10];
idx = ((a == 10).*[1:numel(a)]);
idx(idx > 0)
If you want to use this with a matrix, you will need to use the reshape function to convert it to a linear vector, and then the ind2sub function to get the row and column indices.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by