2

I need to calulate the distance of all points stored in an array which size is <17642065x2 double> with the all points of other array which size is <273839x2 double>. The points stored in both array are in form of:

A = 341 45 456 32 987 10 4003 332 ... ... ... ... .... ....

B = 344 67 786 90 1234 47 3456 222 ... ... ... ... .... ....

I have to calculate distance for all points. For example the algorithm calculates the distnace between (341,45) and (344,67), then (341,45) and (780,90), then (341,45) and (1234,47), then (341,45) and (3456,222) then (456 32) and (344,67) and so on. Your help will be greatly appreciated

bajwa77
  • 53
  • 4

2 Answers2

3

You can use Matlab's builtin pdist2 function:

d = pdist2(A,B);
Itamar Katz
  • 9,544
  • 5
  • 42
  • 74
  • Shouldn't one reshape matrices A and B two make them two-dimensional first? ```A2d = reshape( A, 2, length(A)/2 )``` ... – Matthias W. Jan 27 '16 at 12:00
  • Yes, but in the question he wrote `<17642065x2 double>` etc., so I assumed it is already a 2d array – Itamar Katz Jan 27 '16 at 12:24
  • I have used this command but Matlab gives error "Out of memory. Type HELP MEMORY for your options". – bajwa77 Jan 28 '16 at 07:05
  • That's because the answer is too large to be held in your computer's RAM. You should break your arrays to smaller sub-arrays and treat each one separately. – Itamar Katz Jan 28 '16 at 09:49
  • I have solved this error. when I execute command of memory, MATAB gives: Maximum possible array: 8840 MB (9.270e+09 bytes) * Memory available for all arrays: 8840 MB (9.270e+09 bytes) * Memory used by MATLAB: 1809 MB (1.897e+09 bytes) Physical Memory (RAM): 4018 MB (4.213e+09 bytes) * Limited by System Memory (physical + swap file) available. which shows that I have a memory of 6931MB but command pdist2 still gives same error. – bajwa77 Jan 28 '16 at 11:15
  • It might be that some intermediate calculation needs more memory, or that the amount of contiguous memory (which Matlab needs in order to allocate space for arrays) is smaller. Anyway, there's not much you can do unless you install more RAM. The simplest thing is to break down the arrays as I suggested. – Itamar Katz Jan 28 '16 at 11:58
1

I think the answer of matheburg on this question( Efficiently compute pairwise squared Euclidean distance in Matlab) answers your question. It is even faster than pdist2, which can be useful when you have to do this computation on multiple arrays.

Community
  • 1
  • 1
YV3003
  • 25
  • 5
  • I have implemented the code proposed by matheburg but Matlab gives me error which is " Error using * MTIMES is not fully supported for integer classes. At least one input must be scalar.To compute elementwise TIMES, use TIMES (.*) instead. Give me suggestion how to resolve this problem – bajwa77 Jan 28 '16 at 10:53
  • I tried the following code based on part of your matrices and had no problems (the output is a 3x3 matrix) : d=2;numA=3; numB=3: A=[341 45;456 32;987 10]; B=[344 67; 786 90; 1234 47]; helpA=zeros(numA,3*d); helpB=zeros(numA,3*d); for idx = 1:d helpA(:,3*idx-2:3*idx) = [ones(numA,1), -2*A(:,idx), A(:,idx).^2 ]; helpB(:,3*idx-2:3*idx) = [B(:,idx).^2 , B(:,idx), ones(numB,1)]; end distMat = helpA * helpB'; The output is distMat=[ 493 200050 797453; 13769 112264 605509; 416698 46801 62378] – YV3003 Jan 28 '16 at 22:20
  • Did u run this code on the above mentioned size of matrix?? as when I run this code on that matrix it gives me error of CAT arguments dimensions are not consistent. – bajwa77 Feb 02 '16 at 10:21
  • also can you please explain working of code? I'll be really thankful to you – bajwa77 Feb 03 '16 at 08:50
  • can you tell me your email ID so that I would be able to share my data with you. – bajwa77 Feb 03 '16 at 09:33