Find value in an NxMxP array and return index

6 次查看(过去 30 天)
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?

回答(1 个)

Stephen23
Stephen23 2017-1-5
编辑:Stephen23 2017-1-5
>> 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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by