How to create an array of vectors corresponding to a particular point

27 views (last 30 days)
Hi everyone,
I have been working on this problem for a while and have found no feasible way of doing it.
I have a sphere of points defined by the coordinates X,Y and Z. For each of the point I would like to have a set of complete (i.e. vectors at each X,Y and Z point) vectors that points toward it. For example I can do this quite easily for one point:
R=22; phi=linspace(0,pi,50); theta=linspace(-pi/2,pi/2,50);
[phi,theta]=meshgrid(phi,theta);
X=R*sin(phi).*cos(theta); Y=R*sin(phi).*sin(theta); Z=R*cos(phi);
P1x=22;P1y=0;P1z=0;
X1=P1x-X; Y1=P1y-Y; Z1=P1z-Z;
quiver3(X,Y,Z,X1,Y1,Z1)
As you can see this produces an array of vectors that points towards one point. I would like to do this for every point on the sphere. I'm unsure how to do this though because as I understand it you cannot have an array of arrays in matlab.
If anyone has any ideas they would be appreciated, thanks, John.
  1 Comment
Guillaume
Guillaume on 2 Feb 2016
You can have an array of arrays in matlab, either using a cell array, or if all the arrays are the same size by concatenating them along an extra dimension.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 2 Feb 2016
If I understood correctly, you have X, Y, and Z generated by:
R=22; phi=linspace(0,pi,50); theta=linspace(-pi/2,pi/2,50);
[phi,theta]=meshgrid(phi,theta);
X=R*sin(phi).*cos(theta); Y=R*sin(phi).*sin(theta); Z=R*cos(phi);
From that, you want for each triplet (X, Y, Z) to generate all the (X'-X, Y'-Y, Z'-Z) from all the other triplets. This can be easily achieved:
Xv = bsxfun(@minus, X(:), X(:)');
Yv = bsxfun(@minus, Y(:), Y(:)');
Zv = bsxfun(@minus, Z(:), Z(:)');
Each row or column of (Xv, Yv, Zv) is one of the (X,Y,Z) point.
So for example, if you want to see the vectors for the nth (X, Y, Z) triplet:
n = 20; %for example
quiver3(X(:), Y(:), Z(:), Xv(:, n), Yv(:, n), Zv(:, n));
You can even animate it:
for tripletidx = 1:numel(X)
quiver3(X(:), Y(:), Z(:), Xv(:, tripletidx), Yv(:, tripletidx), Zv(:, tripletidx));
drawnow;
end

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!