필터 지우기
필터 지우기

finding the closest grid

조회 수: 5 (최근 30일)
Matt Learner
Matt Learner 2012년 2월 16일
편집: Daniel 2013년 10월 15일
I have 2 variables and each of 3 discretization say for example, first variable have values (2,4,6) and second variable have (1,3,5). so out of these I can make a 9 combination which looks something like this: grid = {(2,1), (2,3), (2,5), (4,1), (4,3), (4,5), (6,1), (6,3), (6,5)}
Suppose if I have a combination (2.8, 4.2) and I would like to find the closest value out of my 9 combination for it
How can I do that ?
Thanks

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 16일
nearestx = interp1([2,4,6], [2,4,6], comb(:,1), 'nearest');
nearesty = interp1([1,3,5], [1,3,5], comb(:,2), 'nearest');
There are other ways as well.
  댓글 수: 2
Matt Learner
Matt Learner 2012년 2월 17일
Please can you let me know what are the other ways. Cos, I have around 64 combinations obtained from 2 variables and I have to select the nearest combination (out of 64) for the given combination
Walter Roberson
Walter Roberson 2012년 2월 17일
The above will handle that situation as well.
var1vals = sort([2, 4, 6, 7, -3, 11, pi^2, sqrt(30)]);
var2vals = sort([1, 3, 5, 9, 1.15, 33, -5, -8]);
nearestx = interp1(var1vals, var1vals, comb(:,1), 'nearest');
nearesty = interp1(var2vals, var2vals, comb(:,2), 'nearest');
An alternative:
var1vals = sort([2, 4, 6, 7, -3, 11, pi^2, sqrt(30)]);
var2vals = sort([1, 3, 5, 9, 1.15, 33, -5, -8]);
[mindiffx, minidxx] = min(bsxfun(@(a,b) abs(a-b), comb(:,1), var1vals), [], 2);
[mindiffy, minidxy] = min(bsxfun(@(a,b) abs(a-b), comb(:,2), var2vals), [], 2);
nearestx = var1vals(minidxx);
nearesty = var2vals(minidxy);
Note that both of these approaches handle all of the combinations at the same time, if the combinations are stored in the matrix "comb" with the first column being the first variable and the second column being the second variable.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by