How to create an array of vectors corresponding to a particular point
27 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2016-2-2
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.
采纳的回答
Guillaume
2016-2-2
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
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!