Find value in an NxMxP array and return index
1 view (last 30 days)
Show older comments
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?
0 Comments
Answers (1)
Stephen23
on 5 Jan 2017
Edited: Stephen23
on 5 Jan 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.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!