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);
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));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.