필터 지우기
필터 지우기

find nearest value on matrix

조회 수: 2 (최근 30일)
mohamed azuan wahari
mohamed azuan wahari 2016년 5월 9일
댓글: Walter Roberson 2020년 8월 11일
find nearest value on matrix
Can someone help with this problem please..... for example a= 1250
matrix_b=[98 125 945 1005; 105 204 1105 1249; 200 250 1299 1450; 300 450 1350 1850]
want to find nearest equal or greater than a=1250 and the answer should be 1299

답변 (2개)

Roger Stafford
Roger Stafford 2016년 5월 9일
result = min(b(b>=1250));
For this to work, there has to be at least one element of b that is greater than or equal to 1250.
  댓글 수: 1
Image Analyst
Image Analyst 2016년 5월 9일
Or, using the poster's names for variables:
closestValue = min(matrix_b(matrix_b >= a));

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 5월 9일
편집: Walter Roberson 2020년 8월 11일
v = sort(matrix_b(:));
vidx = floor(interp1(v, 1:length(v), a, 'linear', 'extrap'));
if vidx ~= 0
result = v(vidx);
end
Or
v = sort(matrix_b(:));
vidx = find(v >= a, 1, 'first');
if ~isempty(vidx)
result = v(vidx);
end
or
v = sort(matrix_b(:));
[~, vidx] = histc(a, v);
if vidx ~= 0
result = v(vidx);
end
The middle of these is probably the easiest to think about, but it cannot be generalized to a list of a values like the other ones can (aside from the testing to be sure that a result was found.)
  댓글 수: 4
Walter Roberson
Walter Roberson 2020년 8월 11일
The first of them did have a mistake, which I have now corrected.
Could you give an example that fails for the others?
Walter Roberson
Walter Roberson 2020년 8월 11일
Another approach:
v(interp1(v,1:length(v), a, 'nearest', 'extrap'))

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by