MATLAB Answers

0

Does 'i' after value stand for 'index', and if so why does it appear as part of the answer?

Asked by Alexander Eaton on 24 Mar 2018
Latest activity Commented on by Alexander Eaton on 25 Mar 2018
I am trying to calculate the Euclidean distance between two images vectors of 4096 values that are contained in a file called allData (see pic) with the command:
Dist = sqrt(sum((allData(1) - allData(3) .^ 2)))
the answer that was returned was
0.0000 +19.1794i
does this answer seem correct or is the 'i' returned because im indexing into a matrix and im just getting returned one of the values from that image vector or am i being returned the distance between the two rows?

  0 Comments

Sign in to comment.

1 Answer

Answer by Roger Stafford on 24 Mar 2018
Edited by Roger Stafford on 24 Mar 2018
 Accepted Answer

The 'i' in your result indicates a complex answer. I assume you are familiar with complex numbers. The 'i' represents the square root of minus one. This is caused by your attempt to take the square root of a negative number. Apparently allData(1) is less than the square of allData(3).

  5 Comments

thank you for that, when I initilly tried to run it, it questioned me and asked if I meant with the extra parentheses, I presumed it was needed to run the dist calc, but I didnt think I should try running it again. That gave a much more sensible answer, now, for the next step, I need to see how to compare each row with each other for the same calculation. Once again,thank you very much.
One simple way is to create a 208-by-208 matrix in which to store all the possible row pair distances:
n = size(allData,1);
D = zeros(n);
for i1 = 1:n
for i2 = 1:n
D(i1,i2) = sqrt(sum((allData(i1,:)-allData(i2,:)).^2));
end
end
Another way is to use the Matlab function 'pdist' which you can read about at: https://www.mathworks.com/help/stats/pdist.html
As to 'complex' numbers, there is an entire mathematical discipline built around them, and much of modern science would probably collapse without them. Ordinary numbers that you learned about in grade school, which in mathematics are referred to as "real" numbers, are those that can be visualized as points along a straight line extending infinitely far in both the positive and the negative directions. Complex numbers can be visualized as points in an infinite plane in which the real numbers are those along the "real" line, and points off this line are the complex numbers. A special complex number is designated as "i" and is the square root of minus one, so that i^2 = -1. Any point in the plane can then be represented in the form "a+b*i" where a and b are real numbers and are analogous to x, y Cartesian coordinates. The four basic arithmetic operations of addition, subtraction, multiplication, and division can then be easily defined. For example, the product of a+b*i and c+d*i where a, b, c, and d are real is given by:
(a+b*i)*(c+d*i) = a*c+a*d*i+b*c*i+b*i*d*i =
a*c+a*d*i+b*c*i+b*d*(-1) = (a*c-b*d)+(a*d+b*c)*i
(The Matlab language is built to handle complex numbers, as you have discovered.)

Sign in to comment.