Sorting arrays after finding closest values

4 ビュー (過去 30 日間)
Ralph Rodrigues
Ralph Rodrigues 2021 年 3 月 8 日
コメント済み: Ralph Rodrigues 2021 年 3 月 9 日
Hi, I am trying to find a way to find the closest 2,3,4 values inside two sets of (x,y) values to each number in a set of three values. The three values are X values so I just need to find the closest X values and then resize the X and the Y arrays. I was thinking of using the knnsearch function but I'm not sure if that works since the three values (x1 array) would not match up with the 6 values.
x = [1 2 3 5 7 8]; % x values
y = [3 6 19 99 291 444]; % y values
x1 = [2.8 4.4 7.1]; % find the closest 2,3,4 x points to each x1 and then resize x and y arrays
% into 2,3,4 values
%% so for 2.8 (2 closest), x = [2 3] , y =[6 19]
%% 2.8 ( 3 closest), x = [1 2 3], y = [3 6 19]

採用された回答

Image Analyst
Image Analyst 2021 年 3 月 8 日
Try this:
x = [1 2 3 5 7 8]; % x values
y = [3 6 19 99 291 444]; % y values
x1 = [2.8 4.4 7.1]; % find the closest 2,3,4 x points to each x1 and then resize x and y arrays
% into 2,3,4 values
%% so for 2.8 (2 closest), x = [2 3] , y =[6 19]
%% 2.8 ( 3 closest), x = [1 2 3], y = [3 6 19]
for k = 1 : length(x1)
thisX1 = x1(k);
distances = abs(x - thisX1);
[sortedDistances, sortOrder] = sort(distances, 'ascend');
% Find closest 2:
fprintf('\nFor x1 = %.1f, the 2 closet x values are %.1f and %.1f, and the y values are %.1f and %.1f.\n', ...
thisX1, x(sortOrder(1)), x(sortOrder(2)), y(sortOrder(1)), y(sortOrder(2)));
% Find closest 3:
fprintf('For x1 = %.1f, the 3 closet x values are [%.1f, %.1f, %.1f], and the y values are [%.1f, %.1f, %.1f].\n', ...
thisX1, x(sortOrder(1)), x(sortOrder(2)), x(sortOrder(3)), y(sortOrder(1)), y(sortOrder(2)), y(sortOrder(4)));
% Find closest 4:
fprintf('For x1 = %.1f, the 4 closet x values are [%.1f, %.1f, %.1f, %.1f], and the y values are [%.1f, %.1f, %.1f, %.1f].\n', ...
thisX1, x(sortOrder(1)), x(sortOrder(2)), x(sortOrder(3)), x(sortOrder(4)), y(sortOrder(1)), y(sortOrder(2)), y(sortOrder(4)), y(sortOrder(4)));
end
and you'll see
For x1 = 2.8, the 2 closet x values are 3.0 and 2.0, and the y values are 19.0 and 6.0.
For x1 = 2.8, the 3 closet x values are [3.0, 2.0, 1.0], and the y values are [19.0, 6.0, 99.0].
For x1 = 2.8, the 4 closet x values are [3.0, 2.0, 1.0, 5.0], and the y values are [19.0, 6.0, 99.0, 99.0].
For x1 = 4.4, the 2 closet x values are 5.0 and 3.0, and the y values are 99.0 and 19.0.
For x1 = 4.4, the 3 closet x values are [5.0, 3.0, 2.0], and the y values are [99.0, 19.0, 291.0].
For x1 = 4.4, the 4 closet x values are [5.0, 3.0, 2.0, 7.0], and the y values are [99.0, 19.0, 291.0, 291.0].
For x1 = 7.1, the 2 closet x values are 7.0 and 8.0, and the y values are 291.0 and 444.0.
For x1 = 7.1, the 3 closet x values are [7.0, 8.0, 5.0], and the y values are [291.0, 444.0, 19.0].
For x1 = 7.1, the 4 closet x values are [7.0, 8.0, 5.0, 3.0], and the y values are [291.0, 444.0, 19.0, 19.0].
  3 件のコメント
Image Analyst
Image Analyst 2021 年 3 月 9 日
You'd have to sort them again after they're extracted so that they're sorted by the extracted x value rather than the distances.
% Extract the 2 closest values from x.
closestX = x(sortOrder(1:2))
closestY = y(sortOrder(1:2))
% Now sort these two values.
[closestX, sortOrder] = sort(closestX, 'Ascend');
closestY = closestY(sortOrder); % Sort the same way that we did for x.
Ralph Rodrigues
Ralph Rodrigues 2021 年 3 月 9 日
Okay, that makes sense, thank you for your time!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSparse Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by