Find value in an NxMxP array and return index
조회 수: 1 (최근 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?
댓글 수: 0
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!