Using NaN in indexing a matrix

1 view (last 30 days)
Thang  Le
Thang Le on 3 May 2013
Hi all,
I'm writing a function that defines a sphere (center and radius are part of the inputs of the function), takes an fMRI scan file then finds the average activation value of all the voxels within that sphere. Below is what I do:
%Use make_sphere function to make a sphere with given s (grid size) and r (radius):
my_sphere = make_sphere(s,r)
%center of sphere:
center = [i, j, k];
my_sphere = bsxfun(@plus, my_sphere, center); %create a 3D matrix for sphere
%Access the scan file
Data2Read=fullfile(datadir,fname);
a=spm_vol(Data2Read);
v=spm_read_vols(a);
%find the mean activation of voxels within the sphere:
theSum = 0;
count = 0;
for h = 1:size(my_sphere, 2)
theSum = theSum + v(my_sphere(h,1), my_sphere(h,2), my_sphere(h,3));
count = count + 1;
end
MeanActivation = theSum / count
Since it is possible that part of the sphere may fall outside the brain (i.e. outside of the space defined in the scan file), I imagine values in this part of the sphere would be NAN. Thus, when taking the average, I could perhaps use isnan() to exclude these NANs from the average. However, whenever I encounter such scenarios, I only get an error message (Example: ??? Attempted to access v(11,77,63); index out of bounds because size(v)=[72,72,33].). Could anyone help?

Accepted Answer

Image Analyst
Image Analyst on 3 May 2013
No, don't do it that way. See the last chunk of code in this section of the FAQ for how to create a 3D mask of a sphere that you can use to get your mean: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
meanActivation = mean(your3DImage(sphereVoxels));
Like I said, you get sphereVoxels from the FAQ, and in there is where you specify the center and radius.

More Answers (0)

Categories

Find more on Geology in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!