MATLAB Answers

Creating an Array for Different Radial Positions

4 views (last 30 days)
Tom on 7 Aug 2019
Commented: the cyclist on 9 Aug 2019
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.


Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 7 Aug 2019
Edited: the cyclist on 7 Aug 2019
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];


Show 6 older comments
the cyclist
the cyclist on 8 Aug 2019
I just put in an arbitrary calculation that would yield a 3x3 array:
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.
Tom on 9 Aug 2019
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.
the cyclist
the cyclist on 9 Aug 2019
% 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
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.

Sign in to comment.

More Answers (0)


Translated by