Reputation: 191
I want to be able to deal with 3d structures in MATLAB. I'm new to MATLAB, and I haven't seen an answer to this question after googling it.
If you want to define a specific sphere explicitly, without using the built in sphere function, how would this work? For example, is there a way you could define a variable r = (1,1,1) in the xyz coordinate system, and then define a new variable/set S = {all s: distance (r,s) <= radius)}. It would be immensely handy if I could do that, but I'm not sure how MATLAB could deal with something like that, as it would involve an infinite set of points, so MATLAB would have to have some defined maximum resolution. Is this possible? It would be great to be able to define 3d structures in this way. Thank you
Upvotes: 2
Views: 242
Reputation: 112749
If by
all s: distance (r,s) <= radius)
you mean
all s in R3: distance (r,s) <= radius)
then the answer is:
No, you can't define that set by extension (that is, enumerating all its elements), because the set has uncountably infinitely many elements.
But you can define that set S by intension. This means that you can build a rule (a function) that, given any value x in R3, will tell you if x is in S or not.
Namely, that rule can be built using an anonymous function as follows:
>> r = [1 1 1]; %// set center
>> radius = 2; %// set radius
>> inS = @(s) sqrt(sum((s-r).^2))<radius;
The function inS
returns true
(1
) if and only if its input belongs to S, and false
(0
) otherwise. For example,
>> inS([0 0 0])
ans =
1
>> inS([3 4 5])
ans =
0
>> inS([pi sqrt(2) exp(-1)])
ans =
0
This is probably the closest you can get to "defining" that set.
If you want to test several values at once, instead of using a loop you can vectorize the function with bsxfun
:
>> inS = @(s) sqrt(sum((bsxfun(@minus, s, r)).^2, 2))<radius;
>> points = [ 0 0 0
3 4 5
pi sqrt(2) exp(-1) ];
>> inS(points)
ans =
1
0
0
Upvotes: 2
Reputation: 1190
You can use code like the following:
r=1;
resolutionPhi=50;
resolutionTheta=60;
phi=linspace(0,pi,resolutionPhi); %linspace gives (50) equally spaced points between (0) and (pi)
theta=linspace(0,2*pi,resolutionTheta);
[phi,theta]=meshgrid(phi,theta); %meshgrid replicates the vectors into a rectangular array
x=r*sin(phi).*cos(theta);
y=r*sin(phi).*sin(theta);
z=r*cos(phi);
You can adjust the resolution by changing the resolution variables.
Upvotes: 0