Matlab calculations of major/minor axis lengths (regiosprops) - matlab

I have been doing a fair bit of searching around for an answer to my issue here.. hopefully someone can tell me where im going wrong.
im trying to replicate matlabs regionprops function to calculate major/minor axis lengths, and think i understand most of what it's doing but im a little lost.
my understanding was that the axis lengths are equal to the eigenvalues of the covariance matrix (of the regions pixel coordinates) but the numbers i am getting are far from what matlab is returning.
so far i am:
1)extracting coordinates as an n-by-2 matrix.
2)subtracting the x/y means from the matrix.
3)calculating covariance matrix (matrix'.matrix)/num_pixels
4)calculating eigenvalues from the covariance matrix
but the values i get are way away from what matlab is returning. am i doing completely the wrong thing here or am i just making a mistake in the working?

Related

calculating galactic space velocities gal_uvw in

Where to find a clear explanation of gal_uvw transformation? I am searching for some figures to derive the matrix of transformation.
https://github.com/segasai/astrolibpy/blob/master/astrolib/gal_uvw.py
I looked at
https://articles.adsabs.harvard.edu/pdf/1987AJ.....93..864J
but the matrix is a inverse and reverse.

Doing PCA and Whitening with matlab

My task is to do PCA and whitening transform with given 2dimentional 5000data.
What I understand with PCA is analyzing the main axis of the data with covariance Matrix's Eigen Vector and rotate the main axis to the x axis!
So here's what I did.
[BtEvector,BtEvalue]=eig(MYCov);% Eigen value and vector using built-in function
I first calculated eigen values and vectors. The result was
BtEvalue=[4.027487815706757,0;0,8.903923357227459]
and
BtEvector=[0.033937679569230,-0.999423951036524;-0.999423951036524,-0.033937679569230]
So I figured out that the main axis will have eigen value of 8.903923357227459 and eigen vector of [-0.999423951036524,-0.033937679569230] which is the second corresponding term.
After then, because it's two dimentional data, I let cos(theta) as -0.9994.. and sin(theta)=-0.033937. Because I thought the main axis of the data(eigen vector [-0.999423951036524,-0.033937679569230]) has to be x axis I made rotational axis R= [cos(-Theta)-sin(-theta);sin(-theta) cos(-theta)]. Let original data sets A=>2*5000, I did A*R to get rotated data.
Also, For whitening case, using Cholesky whitening, I made whitening transformation matrix as inv(Covariance Matrix).
Is there something wrong with my algorithm? Could someone testify if there's error or misunderstanding please? Thank you a lot in advance.
Since your data is two-dimensional, the covariance matrix that you calculated is not accurate. If you only calculate the covariance with respect to one axis (say x), you're assuming that the covariance along the y axis is identity. This is obviously not true. Although you've attempted to address this, there's a sound procedure that you can use (I've explained below).
Unfortunately, this is a common mistake. Have a look at this paper, where it is explained exactly how the covariance should be calculated.
In summary, you can calculate the covariance along each axis (Sx and Sy). Then approximate the 2D covariance of the vectorized matrix as kron(Sx,Sy). This will be a better approximation of the 2D covariance.

matlab scatter3 plot real and imaginary parts over frequency

I've got to vectors called ttre and ttim which contain real and imaginary data over a frequency (from 1 to 64). The fields are looking like this:
ttim 64x10100 single
ttre 64x10100 single
I can easily make a 2D scatter plot of a certain row by using the command
scatter(ttim(40,:),ttre(40,:))
Now, I would like to display all data in a 3D scatter plot where X=real values, Y=imaginary values and Z=[1...64]
I created an array for Z with the number 1 to 64 and copied it to make it the same size as the other variables, by:
z=(1:64)'
z=repmat(z,1,10100)
result:
z 64x10100 double
When I try to plo a 3D scatter plot now, I get the error "Vectors x,yu,z must be of the same size"...however, as far as I understand, they are of the same size.
>> scatter3(ttim,ttre,z)
Error using scatter3 (line 64)
X, Y and Z must be vectors of the same length.
I hope that someone could point me into the right direction here.
Kind regards
scatter3 needs points to plot, so x,yand z should be 1xN , where N is the amount of points your are plotting. I dont know what your data is, so unfortunately I can not help more. Maybe scatter3(ttim(:),ttre(:),z(:)) works, but I do not recommend it for the huge amount of data you have, it may crash your computer.
However, maybe z=1:64 is not the best option. It means that you will have 64 layers (like floors from a building) of scattered data, not sure if that's what you want.

Formula for covariance of stereo triangulation

I am trying to understand the paper Determining an initial image pair for fixing the scale of a 3d reconstruction from an image sequence by Beder and Steffen. They give a formula (Eq. (8)) for the covariance matrix of a triangulated point after stereo matching. However, when I implemented (rather carefully, I think) their method the matrix in question turned out to be non-symmetric, which is a bit of a problem for a covariance matrix.
Has anyone implemented their method before and has insight on what is going on? Perhaps I am missing something glaringly obvious?
EDIT:
Even without implementing anything, formula (8) feels odd. The matrix $N$ defined there simply cannot by symmetric, because is has $AX$ in one place and $X^{T}$ in the corresponding place when transposed. How can $AX=X$ be true?
A X should not be read together.
N(0,0) =
N(0,1) = X
N(1,0) = XT
N(1,1) = 0

How can I plot only real eigenvalues of symmetrical matrix in matlab?

I have created a matrix of potentials for a particle in a square well. When I take the eigenvectors of the matrix, I get mirror images for the first few (about 10) vectors. For example, the first eigenvector is a postive hump but there is also a negative mirror hump underneath. I looked at the output of the first few vectors and it appeared that the sign of the number was merely changing back and forth from positive to negative. For later vectors this is not the case so I cannot just plot every other point of the vectors. I am using the following command to plot eigenvectors.
[V,D] = eig(A);
I do not see imaginary numbers in my output. However, it has been suggested to me that MATLAB may be trying to plot the real and imaginary components of the eigenvectors. I found the following command on this site and thought it would fix my problem assuming my problem is in fact that the real and imaginary components are not being plotted.
A1 = real(V*real(D)/V);
then I plot:
[V,D] = eig(A1);
Nothing has changed and I am confused as to whether I am correctly plotting the real eigenvalues or if there is something else causing these mirror images. Help!
Real symmetric matrices have always only real eigenvalues and orthogonal eigenspaces, i.e., one can always construct an orthonormal basis of eigenvectors.
If your physical system has a spacial symmetry, for instance if you can mirror it about some symmetry axis such that the physics of both systems is the same, then this symmetry is also reflected in the eigenspaces, they will always have even dimension and you can either construct odd and even symmetric eigenvectors or pairs of eigenvectors that are mirror images of each other.
To say more one needs more details about your problem.