Find value in an NxMxP array and return index

1 visualizzazione (ultimi 30 giorni)
Nasser Ramsis
Nasser Ramsis il 5 Gen 2017
Modificato: Stephen23 il 5 Gen 2017
I have an N x M x P array. Its filled with all possible values of a parameter. I'm looking for the value closest to a target I have. I need to find that value and the index of said value.
I've played around with these functions but they don't work the way I want them.
[C, I] = min(min(min(abs(theta-targettheta))));
thetafound=abs(targettheta-C);
this only return the p index not the n or m
[C, I]=min(abs(theta-targettheta),[],3);
this isn't returning just 1 result or the array even.
Any ideas?

Risposte (1)

Stephen23
Stephen23 il 5 Gen 2017
Modificato: Stephen23 il 5 Gen 2017
>> val = 9;
>> X = reshape(1:36,3,3,4) + rand(3,3,4);
>> [C,I] = min(abs(X(:)-val));
>> X(I)
ans = 9.3895
Or with a different value:
>> val = 24;
>> [C,I] = min(abs(X(:)-val));
>> X(I)
ans = 23.642
Note that min returns the linear index, not a subscript index.

Community Treasure Hunt

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

Start Hunting!

Translated by