Creating an Array for Different Radial Positions
显示 更早的评论
I am basically looking to take a point in spherical polar coordinates such that theta = 0, phi = pi/2 and r = 1, then extend outwards at the same angles but increasing the radius by 0.2 each time until a radius of 10 is reached.
Corresponding to each of these points on a radial line, I then need a 3 x 46 array such that each 3 x 1 vector in the array corresponds to the position of each of the consecutive radial points in Cartesian coordinates, what would be the easiest way of doing this? Please let me know if this is not clear.
采纳的回答
the cyclist
2019-8-7
编辑:the cyclist
2019-8-7
theta = 0;
phi = pi/2;
r = 1 : 0.2 : 10;
theta = repmat(theta,size(r));
phi = repmat(phi, size(r));
[x,y,z] = sph2cart(theta,phi,r);
xyz = [x; y; z];
9 个评论
@the cyclist: Yes that works, thanks a lot.
I don't know if this makes sense, but now that I have this 3 x 46 array for the positions, let's say the first vector in the array is p_1, the next one where you move out along the radius by 0.2 is p_2.
I also have a 3 x 100 array, which I am viewing as the position vectors of 100 points (always with radius less than 1). I basically need to create a matrix which looks as follows, where each distance is the row vector obtained by vector subtraction of the two vectors:
[Distance from p_1 to first vector in 3 x 100 array Distance from p_1 to second vector etc ]
[Distance from p_2 to first vector in 3 x 100 array Distance from p_2 to second vector etc ]
[ Distance from p_3 ..... ]
and so on, until all the positions in the 3 x 46 array are covered, let me know if my intention is not clear.
I think it mostly makes sense.
Is the distance for any given pair a 3x1 array? Or you want to sum somehow over that vector?
What size/shape should the final output be? You are going to have 3 * 46 * 100 values.
Yes, the distance is a 3 x 1 array, so it's like the radial vector which you get by going from the position vector p_1 to a vector in the 3 x 100 array via vector addition (or a subtraction, in this case).
In fact, the distance vector rij needs to go into a function, but it is the same function each time, just the distance which is plugged in changes.
J=1/(8*pi*0.7*sqrt(2/pi))*(eye(3)/norm(rij)+(rij'*rij)/((norm(rij))^3));
However, I have just noticed that the function is going to produce a 3 x 3 matrix when you plug in one of the distances, so the final size is going to be 300 x 138, rather than 300 x 46.
There is no requirement on the shape, and it does not need to be a square matrix. Please let me know if this isn't clear what I mean.
Any objection to making a 4-D array?
theta = 0;
phi = pi/2;
r = 1 : 0.2 : 10;
theta = repmat(theta,size(r));
phi = repmat(phi, size(r));
[x,y,z] = sph2cart(theta,phi,r);
xyz = [x; y; z];
% Create other matrix at random
otherMatrix = rand(3,100);
% Initialize distance output array
distanceOutput = nan(46,100,3,3);
for ii = 1:46
for jj = 1:100
distanceOutput(ii,jj,:,:) = xyz(:,ii)*otherMatrix(:,jj)'; % Put your real formula here
end
end
Sorry, could you clarify where the formula goes when you say 'put your real formula here'?
I just put in an arbitrary calculation that would yield a 3x3 array:
xyz(:,ii)*otherMatrix(:,jj)'
but you should put in your actual distance calculation that gives the correct 3x3 array. It might be the subtraction
xyz(:,ii) - otherMatrix(:,jj)'
but I was not sure (and did not carefully look at your calculation of J). I just wanted to show you how to make the structure.
Is there any way of doing it and avoiding a 4D array, I know it might be more efficient, but I basically want the 300 x 138 array, which I can then multiply with a 300 x 1 vector which I have prepared to get my solution to the problem.
% Initialize distance output array
distanceOutput = nan(300,138);
for ii = 1:46
for jj = 1:100
distanceOutput(3*jj-2:3*jj,3*ii-2:3*ii) = xyz(:,ii) - otherMatrix(:,jj)'; % Put your real formula here
end
end
To be clear, what this code is going to do is the following:
For the i'th (of 46) vector in xyz, and the j'th (of 100) vector in otherMatrix, there is an operation that leads to a 3x3 matrix. That 3x3 matrix is going to be placed into the array distanceOutput (like a tile in a rectangular floor), positioned i steps down and j steps to the right.
I hope that makes sense, and is what you want.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Programming 的更多信息
标签
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
