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

1 回表示 (過去 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 Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by