0

Let:

Where D is the dimensions of the matrices. I will compute each E(i,j) and then populate the resulting matrix. This is what I tried to do. A(i) and B(j) are column vectors of 2 matrices. So A(k,i)-B(k,j) means that I am taking the difference between two column vectors but I have to do it for each row which is represented by k.Then square it and sum them up and finally take the sqrt to out put E(i,j). My code doesn't work and I tried for few days. I am new to Matlab as well as programming. Please excuse if the format of overflow is off.

function E1 =emo(X,Y,i,j)

A = X(:,i);
B = Y(:,j);



function L2 = dis(A,B)
n = size(A);


    for i = 1:n
       C(i,1) = (A(i,1)-B(i,1))^2;
    end

  d = sum(C);
  L2 = sqrt(d);
Community
  • 1
  • 1
user5184
  • 43
  • 7
  • 1
    What is the question? Also side note: LaTeX isn't supported in SO. The math exchange however does support that. – Rollen Mar 10 '15 at 20:13
  • Thank you very much. I spent hours trying to make it work. My question is I want to find the distance of 2 matrices with the same dimension without using "pdist". I will edit it now again. @RollenD'Souza – user5184 Mar 10 '15 at 20:54
  • See if [this](http://stackoverflow.com/questions/23911670/efficiently-compute-pairwise-squared-euclidean-distance-in-matlab) works for you? – Divakar Mar 10 '15 at 21:40
  • 1
    @Divakar - Was about to link them there! – rayryeng Mar 10 '15 at 21:41
  • @rayryeng how did you make it look like Latex? I first wrote everything on my Latex and then copied and pasted it but nothing worked? How can one initiate latex form in overflow? – user5184 Mar 10 '15 at 21:43
  • @Divakar I actually wanted to be original and do it in my way. I was told my approach is right but just can't get it to work. By your experience is it possible? – user5184 Mar 10 '15 at 21:44
  • @user5184 - I used Codecogs: http://www.codecogs.com/latex/eqneditor.php - It is an online LaTeX interpreter where you put in your syntax and an image is rendered. I placed that resulting image in your post. – rayryeng Mar 10 '15 at 21:45
  • @rayryeng Thank you very much sir. I looked at the code you guys sent me but is there a way to do it my way? I really want it to be done in a way I tried. – user5184 Mar 10 '15 at 21:51

1 Answers1

0

I'll go through your code making slight corrections, even though it'd be better to rewrite it substantially.

First of all, your function emo should call the function dis with parameters A,B, and return the result as E1:

function E1 = emo(X,Y,i,j)
  A = X(:,i);
  B = Y(:,j);
  E1 = dis(A,B); 
end 

Second, in the function dis the parameters A,B are column vectors -- not matrices. It's more logical to use a single index to access their entries. Also, it's better to initialize C ahead of time, with zeros -- so that memory is allocated to it.

function L2 = dis(A,B)
  n = numel(A);
  C = zeros(size(A));
  for i = 1:n
    C(i) = (A(i)-B(i))^2;
  end
  d = sum(C);
  L2 = sqrt(d);
end 

Here is the complete script for running the above to make sure it works. I attached the function "testing" in front so that it calls emo with random parameters.

function testing
  X = rand(3,4);
  Y = rand(3,4);
  disp(emo(X,Y,2,3));
end 

function E1 =emo(X,Y,i,j)
  A = X(:,i);
  B = Y(:,j);
  E1 = dis(A,B); 
end 

function L2 = dis(A,B)
  n = size(A);
  C = zeros(1,n);
  for i = 1:n
    C(i) = (A(i)-B(i))^2;
  end
  d = sum(C);
  L2 = sqrt(d);
end 

That said, the whole thing could be written in one line using norm:

E1 = norm(X(:,i)-Y(:,j));