Creating a spherical matrix

조회 수: 2 (최근 30일)
Julia Sandell
Julia Sandell 2013년 1월 16일
편집: Chen 2021년 1월 25일
I am attempting to run a monte carlo simulation modeling the photon transport in a sphere with two different layers. To run this code I need to generate a segmented sphere. The 100x100x100 slab example with four different layers looks like this:
seg = zeros(100,100,100);
seg(:,:,10:25) = 1;
seg(:,:,26:50) = 2;
seg(:,:,51:75) = 3;
seg(:,:,76:end) = 4;
fid = fopen('seg.bin', 'wb');
fwrite(fid, seg, 'uint8');
fclose(fid);
How would I do something similar for a 50x50x50 sphere and an ellipsoid. The commands sphere() and ellipsoid() give me the coordinates for such a shape in an array, but I need to assign numbers as in the example above to a spherical volume.
Thank you so much!

답변 (1개)

Teja Muppirala
Teja Muppirala 2013년 1월 16일
Something like this?
Rx = 1; %<-- Change these to make an ellipse instead
Ry = 1; %<-- Change these to make an ellipse instead
Rz = 1; %<-- Change these to make an ellipse instead
[X,Y,Z] = ndgrid(linspace(-Rx,Rx,50),linspace(-Ry,Ry,50),linspace(-Rz,Rz,50));
R = sqrt((X/Rx).^2 + (Y/Ry).^2 + (Z/Rz).^2);
seg = zeros(size(X));
seg(R <= 1 ) = 1;
seg(R <= 0.5) = 2;
slice(Y,X,Z,seg,0,0,0);
axis equal vis3d; colorbar;
  댓글 수: 2
Julia Sandell
Julia Sandell 2013년 2월 20일
Thank you so much! This exactly what I needed!
Chen
Chen 2021년 1월 24일
편집: Chen 2021년 1월 25일
Hi Teja,
is there anyway to make an ellipsoid insteand of a perfect sphere? I tried to change Rx, Ry and Rz, but it turns out I always get the same sphere...? Can you give an example?
Thanks in advance!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by