필터 지우기
필터 지우기

Using NaN in indexing a matrix

조회 수: 3 (최근 30일)
Thang  Le
Thang Le 2013년 5월 3일
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?

채택된 답변

Image Analyst
Image Analyst 2013년 5월 3일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Parallel Server에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by