Get nearest point on a sphere in MATLAB - matlab

I want to get the point on a n-dimensional sphere, which is nearest to a given point p with the same dimension.
My sphere is created by the dot-product of a positive definite Matrix A.
All points on the sphere are characterized by:
x'*A*x = const
Mathematically, I am searching for the x where
(p-x)'*A*(p-x)
is minimal. (x is a point on the sphere)
I didn't find any MATLAB function for this problem, nor do I have an idea on how to solve this problem in n dimensions.

Related

Calculating mean shift vector in MATLAB

I'm looking for an elegant way to calculate the mean shift vector for a uint8(960x540x3) image in MATLAB. The meanshift vector is given by
S_h is the neighborhood we are looking in given by a circle of radius h. In MATLAB I have built logical mask (s_mask) with those properties.
w(x_i) is the value of the probability map for a pixel with x-y coordinates. This is a scalar value. w is a 960x540 matrix.
x is the center of the neighborhood circle. I'm interested in finding a fast method in calculating such a vector with preferably no for loops but just matrix multiplications.

How to order one dimensional matrices base on values

I want to determine a point in space by geometry and I have math computations that gives me several theta values. After evaluating the theta values, I could get N 1 x 3 dimension matrix where N is the number of theta evaluated. Since I have my targeted point, I only need to decide which of the matrices is closest to the target with adequate focus on the three coordinates (x,y,z).
Take a view of the analysis in the figure below:
Fig 1: Determining Closest Point with all points having minimal error
It can easily be seen that the third matrix is closest using sum(abs(Matrix[x,y,z])). However, if the method is applied on another figure given below, obviously, the result is wrong.
Fig 2: One Point has closest values with 2-axes of the reference point
Looking at point B, it is closer to the reference point on y-,z- axes but just that it strayed greatly on x-axis.
So how can I evaluate the matrices and select the closest one to point of reference and adequate emphasis will be on error differences in all coordinates (x,y,z)?
If your results is in terms of (x,y,z), why don't evaluate the euclidean distance of each matrix you have obtained from the reference point?
Sort of matlab code:
Ref_point = [48.98, 20.56, -1.44];
Curr_point = [x,y,z];
Xd = (x-Ref_point(1))^2 ;
Yd = (y-Ref_point(2))^2 ;
Zd = (z-Ref_point(3))^2 ;
distance = sqrt(Xd + Yd + Zd);
%find the minimum distance

random distribution on n-dimensional cube in matlab

How can i generate random-uniform points in the surface of a N-dimensional cube with edge E?
There is a code for generating for a N-dimensional sphere, but I can't figure it out how can I generate it for a cube.
The nice thing with the N-dimensional hypercube is that its faces are hypercubes of dimension (N-1). Therefore I would proceed in four steps steps.
Draw a random integer called d in the range 1..N to select the hypercube face direction. d=randi(N)
To select a specific face among the two possible ones, draw a random integer called s which can take either of the two values: 0 or 1. s =randi(2)-1
Draw a random uniformly distributed vector called v of length N in the range 0..1. v=rand(N,1)
replace s as the d-th coordinate in v and multiply the result by the edge length E. v(d)=s, v=E*v
Plotting 1000 points on the surface or the 3-d cube of edge-length 2 would we something like:
N=3;
E=2;
Nsamples=1000;
d=randi(N,1,Nsamples);
s =randi(2,1,Nsamples)-1;
v=rand(N,Nsamples);
for i=1:Nsamples
v(d(i),i)=s(i);
end
v = E*v;
plot3(v(1,:),v(2,:),v(3,:),'.');
This implementation is probably not the best in terms of pure efficiency, but you understand how it works.
Hope this helps.
Adrien.

Matlab: Efficiently do SVD many times? (to triangulate a 3D point cloud)

The context: Performing triangulation on many point pairs, 2d to 3d. The equation I have is:
Mv = 0 with M = [P1 -x1 0] (6x6 matrix) v = [X, lambda1, lambda2]^T (6x1)
[P2 0 -x2]
where P1, P2 are 3x4 projection matrices, x1 and x2 are 2D projections of the 3D point X, and the lambdas are just parameters for representing the line-plane intersection (not important). All of this is done in homogeneous coordinates, so M is 6x6.
The problem: By doing a SVD on M, I can get a least squares triangulation of the two 2D points x1 and x2 to get a 3D point. If I repeat this on all point pairs I can obtain a point cloud. However, it is extremely inefficient to repeatedly compute SVD that many times in a for loop; mainly, I would have to construct each matrix by inserting the points first, then doing SVD. Is there a way I can vectorize the SVD computation on a list of point pairs?
Any suggestions would be greatly appreciated!
There is now a triangulate function in the Computer Vision System Toolbox.

Sampling uniformly from many circles on the sphere efficiently in matlab

I have a 3-by-N matrix X whose columns are vectors on the unit sphere (i.e., the Euclidean length of each vector is 1), and I have a 1-by-N vector Theta whose entries are all angles between 0 and pi. For each i, there is a circle on the sphere centered at X(:,i) defined as the set of all points that have the angle Theta(i) with X(:,i). I would like to get one uniform sample from the circle for each i, avoiding for loops because they can be slow in Matlab. I know that in vectorized Matlab code I can easily get one sample each from all circles with angles in Theta if I assume the center of all circles is [0,0,1], and then I know how to get a rotation matrix (using Rodrigues rotation formula) that rotates [0,0,1] to another desired vector x, so for each i, I can just apply this rotation matrix to the sample point I obtained assuming [0,0,1] was the center.
I would like to this for all i without for loops, i.e. using array/matrix/vector notation.
If you're using Rodrigues' rotation formula, you're trying to convert from axis-angle representation to rotation matrices. You're in luck. I happen to have written fast vectorized code to do exactly what I believe you're asking about. You can can find the code here: axang2rotmat.m. Use is pretty straightforward (read the help):
n = 1e3; % Number of axis-angles and rotation matrices
th = pi*rand(1,n); % Random rotation angles between 0 and pi
v = normc(rand(3,n)); % Random rotation vectors, normalized across columns
R = axang2rotmat(v,th); % Generate n rotation matrices, R is 3-by-3-n
Note, the above code is just to demonstrate the use of axang2rotmat and won't give you uniformly sampled rotation matrices (See Miles, Biometrika 1962 for details on why and workaround). I recommend that you calculate random rotation matrices directly, however. You can us another of my functions for that: randrotmat.m.
I also have code to convert back from rotation matrices to axis-angle and check if a particular matrix is a rotation matrix here.