Effacer les filtres
Effacer les filtres

How do I find the closest points in a matrix

6 vues (au cours des 30 derniers jours)
abdul wahab  aziz
abdul wahab aziz le 28 Août 2016
Réponse apportée : John BG le 29 Août 2016
I have a matrix of 1682 rows. For understanding I am giving you a small example to achieve my desired task:
A=[1 2 4 5 100;
2 3 4 5 1;
3 4 5 6 7;
2 2 3 4 4;
1 1 1 2 2 0;
0 1 0 1 2]
OK first I will look for a vector where 100 exists -- here it is first column. Now what I need is its closest vectors in sorted form. I can use any number to change the closest vectors, so if it's 1 then it must return the min distance, but if its 3 it must return the closest 3 distances from that vector which i searched for.
For my task, I need minimum 10 closest distance points. I must know their column numbers for referencing.
Kindly help me in this task.
  1 commentaire
Image Analyst
Image Analyst le 28 Août 2016
100 is in column 5, row 1, NOT in column 1. Also, why does row 5 have 6 columns instead of 5? Assuming you meant row 1 instead of column 1 for the 100, the row vector with 100 in it is [1,2,4,5,100]. Now, I don't know what you mean when you say you must compute the distance to other vectors and determine the closest. How are you computing the distances? Are you using sqrt of the sum of the squares? Or some other metric like city block distance?

Connectez-vous pour commenter.

Réponses (3)

Walter Roberson
Walter Roberson le 28 Août 2016

Image Analyst
Image Analyst le 28 Août 2016
Try this code:
clc;
% A=[1 2 4 5 100;
% 2 3 4 5 1;
% 3 4 5 6 7;
% 2 2 3 4 4;
% 1 1 1 2 2;
% 0 1 0 1 2]
% Make sample data of 1682 points in 5 dimensions.
A = randi(100, 1682, 5);
distances = pdist2(A, A);
% Get size of matrix.
[rows, columns] = size(A);
% For each point in 5-D space (each row)
% in order of increasing distance.
[sortedDistances, sortOrder] = sort(distances, 2);
% Crop off first distance since the distance of
% a point to itself is always 0.
% And crop off the 11th and farther distances
% since we only want the 10 closest.
% Columns 2-10 will be the 10 closest.
sortedDistances = sortedDistances(:, 2:11);
% Also extract the column indexes for those to cloests.
sortOrder = sortOrder(:, 2:11);
% Now, for example, find the 10 closest rows to row #13
theseDistances = sortedDistances(13,:);
theseRows = sortOrder(13, :);
% Print them
for otherRow = 1 : 10
fprintf('Row #%4d is %5.2f away from row #13\n', theseRows(otherRow), theseDistances(otherRow));
end
msgbox('Done with demo!');
You will see:
Row #1388 is 16.88 away from row #13
Row #1183 is 19.26 away from row #13
Row #1552 is 19.47 away from row #13
Row #1200 is 20.15 away from row #13
Row # 337 is 20.52 away from row #13
Row # 508 is 21.14 away from row #13
Row # 948 is 25.28 away from row #13
Row # 396 is 25.65 away from row #13
Row #1357 is 26.55 away from row #13
Row # 477 is 27.31 away from row #13
which gives the distances of the 10 closest other rows to row #13.
  2 commentaires
abdul wahab  aziz
abdul wahab aziz le 28 Août 2016
thanks for your answer but i asked for columns and where i am giving my desired col vector from which i will compare?
Image Analyst
Image Analyst le 29 Août 2016
Did you see this code in there:
% Now, for example, find the 10 closest rows to row #13
theseDistances = sortedDistances(13,:);
theseRows = sortOrder(13, :);
As an example I chose row vector #13. You can pick a different row if you want.
I don't know what you mean by saying "column vector". Again, what is your definition of distance? Do you have a vector in 1682 dimensions? That seems highly doubtful. Well, column vectors are 1682 rows long so if this were a vector, it would define a point in 1682-space. Just like 2 numbers (x,y) define a point in 2-D space, and 3 numbers (x,y,z) define a point in 3-dimensional space, a vector of 1682 numbers would define a single point in 1682-dimensional space. Is that really what you want? You want to compare the distance in 1682-space from each point (which is a column) to every other point (the other columns)? It can be done but I doubt that's what you really want.

Connectez-vous pour commenter.


John BG
John BG le 29 Août 2016
Hi Abdul
please check if the following answers your question
function void
A=[1 2 4 5 100;
2 3 4 5 1;
3 4 5 6 7;
2 2 3 4 4;
1 1 1 2 2 ;
0 1 0 1 2]
%find vector containing target 100
[i j]=ind2sub(size(A),find(A==100))
% j contains the column where 100 is, load j column in reference vector L0
L0=A(:,j)
ind=[1:1:j]
ind(j)=[] % pointers to all vector columns except the one containing 100
v_min_dist=norm(A)
p=0
for k=ind
d1=dist1(L0,A(:,k))
P(k)=d1
if d1<v_min_dist
v_min_dist=d1;
p=k;
end
end
function d=dist1(v1,v2)
d=((v1-v2).^2)^.5
end
the loop gradually approaches the closest vector, that here turns out to be column
p =
4
with euclidean distance
d1 =
95.084173236138511
the closest vector is
A(:,p)
ans =
5
5
6
4
2
1
note the function distance I chose euclidean but you can change to any distance algorithm you find convenient
You mentioned you want to repeat the search for closest vector, for instance 3 times.
To find the 3 closest vectors do the following
[B,I]=sort(P)
99.005050376230813
I =
4 3 2 1
I contains the vectors sorted out. The first vector is the closest one to L0, the second element of I is the 2nd closest vector to L0 and so on.
If you want the N=3 closest vectors
N=3
A(:,I([1:N]))
=
5 4 2
5 4 3
6 5 4
4 3 2
2 1 1
1 0 1
these are the 3 closest vectors to the one containing your target 100.
Worth mentioning that you didn't comment on the possibility to have 2 vectors in A containing 100.
Abdul
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by