comparing numbers in matlab - matlab

I am trying to classify some data based on euclidean distances in matlab the only problem is that matlab is giving me numbers that look like these as distances
0 + 4.9713i
0 + 7.8858i
num1<num2
num2<num1
both return 0. how is this possible?

The numbers you're getting are imaginary numbers. You should never obtain imaginary numbers when you calculate Euclidean distances.
Check that your Euclidean distances are correct, such as
distance=sqrt(deltaX.^2 + deltaY.^2)
If you're really sure that your distances should be complex numbers, make the comparison using e.g. the norm, i.e.
norm(num2) > norm(num1)
This evaluates to true for me.

Numbers with real and imaginary parts are not orderable. Maybe you mean order by distance from origin?

Related

How to transform a decimal number into binary form but still keep distance property?

I am try to transform a decimal number into a binary form but still keep distance information. Such as 10-2=8 in euclidean space, but in binary case, hamming(1010-0010)=1, obviously the distance information lost a lot. Is there any possible way to transform 10 into a binary form but still keep distance property in hamming distance metric? The naive way is hamming(1111111111-0000000011)=8....
You have found what is basically the only distance preserving map from the metric space of nonnegative integers with distance d(x,y) = |x-y| to the metric space of bit vectors with the hamming distance.
This isn't hard to prove: d(x,0) = x, so the transform of x must have x bits set. Similarly, if x<y, then the bits set in the transform of x must be a subset of the bits set in the transform of y.
So what you constructed is basically your only option if you really and truly must work in the space of bit vectors and hamming distance.

How does matlab compare two complex numbers?

I saw a file in matlab with used max() on a matrix whose entries are complex numbers. I can't understand how does matlab compare two complex numbers?
ls1=max(tfsp');
Here , tfsp contains complex numbers.
The complex numbers are compared first by magnitude, then by phase angle (if there is a tie for the maximum magnitude.)
From help max:
When X is complex, the maximum is computed using the magnitude
MAX(ABS(X)). In the case of equal magnitude elements, then the phase
angle MAX(ANGLE(X)) is used.
NaN's are ignored when computing the maximum. When all elements in X
are NaN's, then the first one is returned as the maximum.

Matlab function for creating random number satisfying constraints

As an input I have two number x and y. x>y.
I want to create exactly y non-zero random number which their sum will be equal to x. I know randi([min max]) function . Can you help me?
If I got it right, you want something like this:
data = rand(1,y);
data = data * x / sum(data);
data will contain exactly y positive uniformly distributed numbers which sum equals to x.
Check out the file random vectors generator with fixed sum in Matlab FEX. I believe this will answer your question.
Leonid's approach will certainly generate a set of random numbers that have the correct sum, but it won't select uniformly over the allowed space. If this is important, an approach that will work is the following:
(with x = 1):
Generate Y-1 random numbers uniformly over [0,1].
Sort the Y-1 numbers from smallest to largest. Call these {y1,...,y_{N-1}}
Take as the Y random numbers the set {y_1-0 ,y_2-y1,...,1-y_{N-1}} == {n_1,... n_Y}.
These n_i clearly sum to one. It is easy to prove uniformity by considering the probability for a given realization of the n_i.

Finding the smallest negative eigenvalue if the eigenvalues are complex

I want find in matlab ,the smallest negative eigen value,from complex eigenvalues ,of a squaure matrix (5,5) with all the entries of the matrix are complex .The answer should be real value.So how can I do this im matlab?.
Is it what you need?
min(real(eig(A)));
You cannot compare complex numbers. At most, you can compare the magnitudes of complex numbers.So, min(abs(eig(A))) is the right answer. If you need the negative of this value, just tack on a negative sign

How do I do numerical integration of a vector in MATLAB?

I have a vector of 358 numbers. I'd like to make a numerical integration of this vector, but I don't know the function of this one.
I found that we can use trapz or quad, but i don't really understand how to integrate without the function.
If you know the horizontal spacing of your vector, you can use trapz in order to integrate it without the function. For example, to integrate y=sin(x) from 0 to pi with 358 sections,
x=0:pi/357:pi;
y=sin(x);
area=trapz(x,y);
If you just use trapz(y), you'll get a much larger number, since the default distance between points is assumed to be 1. This problem can be fixed by multiplying by the distance between x points:
area=pi/357*trapz(y);
You don't need to know the function in order to numerically integrate; that's the point of trapz and quad. Just pass trapz your vector. Here's a link to the documentation.
Think about integration as to find area under the curve, which is formed by your vector. Well it's not actually a curve, but polygonal chain. What TRAPZ function is doing, it finds sum of areas of each trapezoids formed by every two neighbor points in your vector and their projection on X axis. See the function documentation, if you have uneven distance between your points or if distance not equal one.
You can read more about this method, for example, on Wikipedia.