Comparing matrices of different length

I have two matrices of different length. For both matrices the first column is x-coordinate, second column is y-coordinate and third column is height. I would like to compare all points for both matrices and for those points where the distance is less than 100 it should calculate the height difference. Is there a smart way of doing this?

3 件のコメント

KL
KL 2017 年 4 月 28 日
編集済み: KL 2017 年 4 月 28 日
Do you want to apply this 'less than 100' condition along both axes?
KSSV
KSSV 2017 年 4 月 28 日
Question need to be more clear. Let A and B be the set of points; pick one point from A..say A(i,:), you want to pick all the points in B which are less then 100 units of distance from Ai..and like for each and very point for A?
JVM
JVM 2017 年 4 月 28 日
I will try to make it more clear. Imagine both matrices being x(first column) and y(second column) coordinates with a given height(third column). I want to compare the two matrices for all points and for those points were the distance is less than 100 i want to calculate the height difference. Notice that the coordinates are not given in lat/lon coordinates but in an metric system.
I have already made a simpel script that works based on two for loops but my two matrices are very large so it takes a lot of time to run. My script is as follows
counter = 0;
A1 = % first matrix 450000x3 double
A2 = % second matrix 650000x3 double
for i = 1:length(A1)
for j = 1:length(A2)
if sqrt((A2(j,1)-A1(i,1))^2+(A2(j,2)-A1(i,2))^2)<100
counter = counter + 1;
height(counter,1) = A1(i,1);
height(counter,2) = A1(i,2);
height(counter,3) = abs(A1(i,3)-A2(j,3));
end
end
end
I haven't written the actual matrices but I hope you get the idea.

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

回答 (2 件)

Stephen23
Stephen23 2017 年 4 月 28 日
編集済み: Stephen23 2017 年 4 月 28 日

0 投票

If the matrices are not large then you can easily use permute and bsxfun. Here I put the A points down the first dimension, and the B points along the second dimension of the output matrices.
>> A = [0,0,1;10,10,0]; % [X,Y,H]
>> B = [1,1,0;11,11,2;111,111,3]; % [X,Y,H]
>> A3 = permute(A,[1,3,2]);
>> B3 = permute(B,[3,1,2]);
>> H = bsxfun(@minus,A3(:,:,3),B3(:,:,3)); % all height differences
Actually all height differences are in H. If you want to identify the distances between the points, then do this:
>> M = bsxfun(@minus,A3(:,:,1:2),B3(:,:,1:2)); % all X & Y differences
>> D = sqrt(sum(M.^2,3)) % euclidean distance from X & Y differences
D =
1.4142 15.5563 156.9777
12.7279 1.4142 142.8356
>> H(D>=100) = NaN % optional
H =
1 -1 NaN
0 -2 NaN

1 件のコメント

JVM
JVM 2017 年 4 月 28 日
編集済み: JVM 2017 年 4 月 28 日
Thanks, but my matrices are unfortunately very large.

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

Stephen23
Stephen23 2017 年 4 月 28 日
編集済み: Stephen23 2017 年 4 月 28 日

0 投票

For large matrices you could (possibly) speed up your code by only using one loop, and vectorizing the operations in the inner loop:
A = randi(10000,450000,3);
B = randi(10000,650000,3);
C = {};
for k = 1:size(A,1)
D = sqrt(...
(B(:,1)-A(k,1)).^2 + ...
(B(:,2)-A(k,2)).^2);
idx = D<100;
C{end+1} = B(idx,3)-A(k,3);
end

2 件のコメント

JVM
JVM 2017 年 4 月 28 日
Of course, I haven't even though about that. I will try to run it now, but isn't there a way to vectorizing both operators?
Stephen23
Stephen23 2017 年 4 月 28 日
編集済み: Stephen23 2017 年 4 月 28 日
If memory was not limited, then the fully vectorized approach would be exactly as I showed in my other answer, using bsxfun. The reason to use one loop is simply because (most likely) you are using a PC and have some GB of memory. Using one loop means that the intermediate arrays are of a size that can actually be stored in memory.

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

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

JVM
2017 年 4 月 28 日

編集済み:

2017 年 4 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by