Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

calculate distance using the below equation

Asked by Elysi Cochin

Elysi Cochin (view profile)

on 11 Apr 2013
Accepted Answer by Matt J

Matt J (view profile)

i have 2 variables, dataMatrix and queryMatrix..... dataMatrix contains 245 rows and 38 column values...... and queryMatrix contains 1 row and 38 column values...... i calculate the euclidean distance and sort the distances in ascending order as below.....

    for w=1:245
        dist = sum((repmat(queryMatrix(w,:),245,1)-dataMatrix).^2,2);
        [sortval sortpos] = sort(dist,'ascend');
        neighborIds(w,:) = sortpos(1:k);
        neighborDistances(w,:) = sqrt(sortval(1:k));
    end

instead of euclidean distance i want to use the distance formula in the below link.....

http://img28.imageshack.us/img28/3595/distanceu.png

but i dont know to write the equation in matlab...... please can someone convert that equation to matlab..... it would be great help to me..... please do reply......

1 Comment

Matt J

Matt J (view profile)

on 11 Apr 2013

If queryMatrix only has 1 row, how can w run from 1 to 245 in queryMatrix(w,:)?

Elysi Cochin

Elysi Cochin (view profile)

Tags

Products

No products are associated with this question.

2 Answers

Answer by Matt J

Matt J (view profile)

on 11 Apr 2013
Accepted answer

Assuming queryMatrix and dataMatrix are both 245x38, the whole thing can be done without loops,

    d=permute(dataMatrix,[1,3,2]);
    q=permute(queryMatrix,[3,1,2]);
    num = abs(bsxfun(@minus,d,q));
    den =  bsxfun(@plus,abs(d),abs(q));
    dist=sum(num./den,3);
    [sortval sortpos] = sort(dist);
    neighborIds = sortpos(1:k,:);
    neighborDistances = sqrt(sortval(1:k,:));

2 Comments

Elysi Cochin

Elysi Cochin (view profile)

on 11 Apr 2013

sir what is 1,3,2 and 3,2,1 in

    d=permute(dataMatrix,[1,3,2]);
    q=permute(queryMatrix,[3,1,2]);

shud i use it in my datavalues???..... as my datavalues are not of the same row and column values.... what changes should i make when i use for loop?? please do reply sir....

Matt J

Matt J (view profile)

on 11 Apr 2013

To understand the syntax of the PERMUTE command, see

 doc permute
 help permute

As I mentioned, you don't need to be using a for-loop. The only requirement here is that dataMatrix and queryMatrix have the same number of columns.

Matt J

Matt J (view profile)

Answer by Andrei Bobrov

Andrei Bobrov (view profile)

on 11 Apr 2013
Edited by Andrei Bobrov

Andrei Bobrov (view profile)

on 11 Apr 2013
dataMatrix = randi([-10,10],10,5);
queryMatrix = randi([-10 10],1,5);
d = bsxfun(@(x,y)abs(x - y)./(abs(x) + abs(y)),...
                                         dataMatrix,permute(queryMatrix,[3 2 1]));
d = sort(squeeze(sum(d,2)));

2 Comments

Elysi Cochin

Elysi Cochin (view profile)

on 11 Apr 2013

sir in my code should i use for loop??? because when i do without for loop i'm getting all values NaN......

 dataMatrix = dataMatrix.*sign(rand(size(dataMatrix)));
 queryMatrix = queryMatrix.*sign(rand(size(queryMatrix))-.5);

shud i use this rand function for my set of values??

Elysi Cochin

Elysi Cochin (view profile)

on 12 Apr 2013

thank u all...

Andrei Bobrov

Andrei Bobrov (view profile)

Contact us