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

조회 수: 9 (최근 30일)
Om
Om 2016년 4월 6일
답변: Om 2016년 4월 7일
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

채택된 답변

Baltam
Baltam 2016년 4월 6일
편집: Baltam 2016년 4월 6일
% 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

추가 답변 (1개)

Om
Om 2016년 4월 7일
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.

카테고리

Help CenterFile Exchange에서 Volume Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by