Calculate distances between data points

2 Ansichten (letzte 30 Tage)
Mani Ahmadian
Mani Ahmadian am 14 Dez. 2014
Kommentiert: matt dash am 15 Dez. 2014
Hi
I have 6 separated groups(as g1,g2,g3,..., g6) and each of them have some spatial data points as (x,y,z,k) where x,y,z determine position of each data point and k is its value, for example Temperature value at that point.
Note that the number of data points in each group is not equal.
I want to calculate distances between data points in each group and other data points in other groups.
Would you please guide me to solve my problem?
Thanks a lot.
Mani
  2 Kommentare
Guillaume
Guillaume am 14 Dez. 2014
So do you want the distance between all of the points in one group and all of the points in another group, that is size(g1, 1) * size(g2, 1) distances, or something else?
Mani Ahmadian
Mani Ahmadian am 14 Dez. 2014
Yes, I do. I want it exactly.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Guillaume
Guillaume am 14 Dez. 2014
Possibly, this is what you want:
g1 = randi([-100 100], 5, 4); %example data, a 5*4 matrix
g2 = randi([-100 100], 3, 4); %example data, a 3*4 matrix
[idx1, idx2] = ndgrid(1:size(g1, 1), 1:size(g2, 1)); %row indices to calculate distance between
distance = arrayfun(@(row1, row2) norm(g2(row2, 1:3) - g1(row1, 1:3)), idx1, idx2)
Each row in the output matrix correspond to a row of g1, and each column to a row of g2. For example, the value at row 3, column 2 is the distance between row 3 of g1 and row 2 of g2.
  2 Kommentare
Mani Ahmadian
Mani Ahmadian am 15 Dez. 2014
I completed a script to solve my problem as bellow:
clc
close all
clear all
% g1=[1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4; 5 5 5 5];
% g2=[6 6 6 6; 7 7 7 7; 8 8 8 8; 9 9 9 9];
% g3=[10 10 10 10; 11 11 11 11];
tic
g1=randi(1000,1000,4);
g1(:,1)=2;
g1(:,2)=2;
g2=randi(1000,1000,4);
g2(:,1)=5;
g2(:,2)=5;
g3=randi(1000,1000,4);
g3(:,1)=13;
g3(:,2)=13;
rows=[size(g1,1),size(g2,1),size(g3,1)];
cols=[size(g1,2),size(g2,2),size(g3,2)];
MaxRows=max(rows);
MaxCols=max(cols);
n=3;
MyNullValue=-123456.123456;
MyTotal=ones(MaxRows,4,n)*MyNullValue;
MyTotal(1:size(g1,1),:,1)=g1;
MyTotal(1:size(g2,1),:,2)=g2;
MyTotal(1:size(g3,1),:,3)=g3;
MyTotal(MyTotal==MyNullValue)=0;
MyDist=ones(MaxRows,MaxRows,n^2)*MyNullValue;
kk=0;
for ii=1:n
for jj=1:n
Mat1=MyTotal(:,:,ii);
Mat1(all(~Mat1,2), : ) = [];
Mat1(:,all(~Mat1,1)) = [];
Mat2=MyTotal(:,:,jj);
Mat2(all(~Mat2,2), : ) = [];
Mat2(:,all(~Mat2,1)) = [];
kk=kk+1;
MyDist(:,:,kk)=distances(Mat1,Mat2,MaxRows);
end
end
MyDist(MyDist==0)=inf;
MinDist=min(min(min(MyDist)))
MyDist(MyDist==inf)=0;
MaxDist=max(max(max(MyDist)))
beep
toc
%'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
function [distance]=distances(g1,g2,MaxRows)
[idx1, idx2] = ndgrid(1:size(g1, 1), 1:size(g2, 1)); %row indices to calculate distance between
distance = arrayfun(@(row1, row2) norm(g2(row2, 1:3) - g1(row1, 1:3)), idx1, idx2);
MyRows=size(distance,1);
MyCols=size(distance,2);
if MyRows<MaxRows
distance=vertcat(distance,zeros(MaxRows-MyRows,MyCols));
end
if MyCols<MaxRows
distance=horzcat(distance,zeros(size(distance,1),MaxRows-MyCols));
end
end
I run my script just for 3 groups of data points instead of 6 groups as above. It takes long about 5 minutes. How can I change my script to become faster than now?
Thanks a lot
Mani
matt dash
matt dash am 15 Dez. 2014
Look on the file exchange for the file named "IPDM" http://www.mathworks.com/matlabcentral/fileexchange/18937-ipdm--inter-point-distance-matrix ... it does what you need and it is very fast:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by