How do i insert one value from one array to another array

5 ビュー (過去 30 日間)
Lars Urban
Lars Urban 2022 年 8 月 2 日
コメント済み: Lars Urban 2022 年 8 月 3 日
I have 2 arrays with coordinates. For example
A = [1,2;2,1;1,1;1,3;3,2]
A = 5×2
1 2 2 1 1 1 1 3 3 2
B = [0.5,0.5;2,2]
B = 2×2
0.5000 0.5000 2.0000 2.0000
I compute for every point in A the nearest Point from B. I use dsearchn
k = dsearchn(B,A)
k = 5×1
2 2 1 2 2
Now I want to give every point in B the next points from A. Making for every point in B a list of nearest points from A. Like point B(2,:) ans = 2 , 2 has the next points A(1,:),A(2,:),A(4,:) and A(5,:). I have no clue how to do it right and efficient.
  2 件のコメント
Rik
Rik 2022 年 8 月 2 日
What is the actual output in terms of Matlab variables you want?
Lars Urban
Lars Urban 2022 年 8 月 2 日
I thought about a structure array where it is divided by coordinate of B in x and y and the a list of double from A.x and A.y

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

採用された回答

Bruno Luong
Bruno Luong 2022 年 8 月 2 日
See if s is what you want or not (not clear for me)
A = [1,2;2,1;1,1;1,3;3,2]
A = 5×2
1 2 2 1 1 1 1 3 3 2
B = [0.5,0.5;2,2]
B = 2×2
0.5000 0.5000 2.0000 2.0000
k = dsearchn(B,A)
k = 5×1
2 2 1 2 2
AA = accumarray(k, (1:size(A,1))', [], @(r) {A(r,:)});
s = struct('B', num2cell(B,2), 'A', AA)
s = 2×1 struct array with fields:
B A
s(1).B
ans = 1×2
0.5000 0.5000
s(1).A
ans = 1×2
1 1
s(2).B
ans = 1×2
2 2
s(2).A
ans = 4×2
1 2 2 1 1 3 3 2
  1 件のコメント
Lars Urban
Lars Urban 2022 年 8 月 3 日
Yes this is what i want at the end. Sorry for the bad description of my problem. Thank you!

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

その他の回答 (1 件)

David Hill
David Hill 2022 年 8 月 2 日
Look at pdist2
A = [1,2;2,1;1,1;1,3;3,2] ;
B = [0.5,0.5;2,2];
pdist2(A,B)
ans = 5×2
1.5811 1.0000 1.5811 1.0000 0.7071 1.4142 2.5495 1.4142 2.9155 1.0000
  2 件のコメント
Lars Urban
Lars Urban 2022 年 8 月 2 日
It would be end in the same problem where i have to sort each point with the closest distance to one point B. I have found w dsearch for each point in A the closest point in B but i want to sum for every point in B their closest points from A.
David Hill
David Hill 2022 年 8 月 2 日
I don't understand your comment. Each column above gives the distance between each point in B with all the points in A. You could sort each column and then index into A to provide the sorted listing of the closest points of A to each point in B.

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

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by