2 views (last 30 days)

I have an issue with distance calculation, and I used the code bellow to calculate it:

k=2;

D=data;

[N m]= size (data);

R_I = randsample(N,k);

Cnt = D(R_I,:);

while(true)

for l=1:N

for i=1:k

dist(:,i) = sqrt( (D - repmat(Cnt(i,:),N,1))*((Sigma)^-1) * (D -repmat(Cnt(i,:),N,1))');

end

end

dist=sqrt(dist);

I'm receiving an error which is:

Subscripted assignment dimension mismatch.

Jan
on 26 Jun 2017

Edited: Jan
on 26 Jun 2017

N is the length of the 2nd dimension of data:

[m N]= size (data);

Then you coose two elements from 1:N:

R_I = randsample(N,k);

But then you use the indices in the 1st dimension:

Cnt = D(R_I,:);

Either

R_I = randsample(m, k);

or

Cnt = D(:, R_I);

Please explain, what you want to achieve. The code can be simplified substantially.

Jan
on 29 Jun 2017

@shawin: Did you recognize the hint ">= R2016b" and used the bsxfun solution?

Now we can run your code and get the same error. But you still did not explain, what you want to achieve. The failing code does not clarify this uniquely.

Image Analyst
on 27 Jun 2017

Edited: Image Analyst
on 27 Jun 2017

John BG
on 29 Jun 2017

Edited: John BG
on 29 Jun 2017

Hi Shawin

correct if wrong, but it seems you are aiming at calculating the dissimilarity between observations and expectations.

From

you want to calculate the distance

d(x,y)=((x-y)'*S^-1*(x-y))^.5

where x and y are vectors that have to be same length. Generating shorter data than the .csv file you have kindly attached

clear all; close all

N=10;a=-10;b=10;

r1x=(b-a)*rand(N,1)+a; r1y=(b-a)*rand(N,1)+a;

r2x=(b-a)*rand(N,1)+a; r2y=(b-a)*rand(N,1)+a;

x1=[r1x r1y];x2=[r2x r2y];

x1 =

-4.9639 8.1262

-4.1912 7.5931

2.3418 6.3552

-4.6944 -4.7854

6.4875 1.8871

9.6533 -9.5497

4.6050 -1.4948

-3.1225 -3.7456

1.6814 -6.7703

-7.8446 -6.4247

x2 =

-1.5423 0.6173

-8.1154 3.0889

1.9705 -1.8476

-0.5815 6.3996

3.9190 4.3672

3.9978 9.3730

2.7706 0.6267

-9.3279 -3.4971

-8.6239 -7.8874

-3.6080 2.2192

S=cov(x1,x2)

S =

35.8692 6.4390

6.4390 27.6440

.

rather than

(S^-1*(x1-x2)')*(x1-x2)

=

7.4389 -5.7005

-2.8055 27.1493

try mahdist and Covariance functions typed following these lines

d1=mahdist(x1,x2)

=

1.2241

if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?

To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link

thanks in advance

John BG

%%%%

the support functions, from from http://people.revoledu.com/kardi/tutorial/Similarity/MahalanobisDistance.html

function d=mahdist(A,B)

[n1 k1]=size(A);

[n2 k2]=size(B);

n=n1+n2;

if (k1~=k2)

disp('number of columns of A and B msut be equal')

else

xDiff=mean(A)-mean(B); % mean diff row vector

cA=Covariance(A)

cB=Covariance(B)

pC=n1/n/n*cA+n2/n*cB % pooled covariance matrix

d=(xDiff*inv(pC)*xDiff').^.5;

end

end

function C=Covariance(X)

[n,k]=size(X)

Xc=X-repmat(mean(X),n,1)

C=Xc'*Xc/n

end

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/346300-issue-with-distance-calculation#comment_464516

⋮## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/346300-issue-with-distance-calculation#comment_464516

Sign in to comment.