Finding locations on a graph (1x1x1 cube) and replacing values with zero

4 views (last 30 days)
Hi all!
I have created this cube with 1000 random points within it:
for i=1:1000
x(i)=rand;
y(i)=rand;
z(i)=rand;
scatter3 (x,y,z,'r');
Now what I need to do is define an area from the centre (centre point being 0.5,0.5) and give them these with 0 values/erase the values within that area.
It isn't the visual side that is important as it is the remaining values that I will be using the chart below is just a way of making it visually make sense!
So what I need is that everything between radius 0.5-0.7 = 'value 1', and radius 0.7-1.0 = value 2

Accepted Answer

Baltam
Baltam on 6 Apr 2016
Edited: Baltam on 6 Apr 2016
% Do not use for loops, you can make a vector of random values in one
% go.
x=rand(1000,1);
y=rand(1000,1);
z=rand(1000,1);
% Define centerpoint
Cpt = [0.5 0.5 0.5];
% Use definition of sphere x^2+y^2+z^2 < Radius^2
% I used two logical statements and used a pointwise product. Similar
% to an && statement. After that you need to convert the values back to
% logicals.
Z1 = logical ( ((x-Cpt(1)).^2+(y-Cpt(2)).^2+(z-Cpt(3)).^2 > 0.5^2) .* ((x-Cpt(1)).^2+(y-Cpt(2)).^2+(z-Cpt(3)).^2 < 0.7^2) );
Z2 = logical ( ((x-Cpt(1)).^2+(y-Cpt(2)).^2+(z-Cpt(3)).^2 > 0.7^2) .* ((x-Cpt(1)).^2+(y-Cpt(2)).^2+(z-Cpt(3)).^2 < 1^2) );
% Ztot contains ones for all points within both spheres but not
% for the values within a radius smaller than 0.5. You can use this to
% plot only these values.
Ztot = logical(Z1+Z2);
% Assign values for certain conditions
value1 = 10;
value2 = 5;
values = NaN(size(x));
values(Z1) = value1;
values(Z2) = value2;
% Plot results
scatter3(x(Ztot),y(Ztot),z(Ztot),[],values(Ztot))
xlim([Cpt(1)-1 Cpt(1)+1])
ylim([Cpt(2)-1 Cpt(2)+1])
zlim([Cpt(3)-1 Cpt(3)+1])
Kind regards, Baltam

More Answers (1)

Om
Om on 7 Apr 2016
Thank you so much! That is exactly what I needed.
So now I need to give a value to the parts that remain within the blank sphere.

Community Treasure Hunt

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

Start Hunting!