Mean of many changing "for loop" 3D matrices

조회 수: 3 (최근 30일)
Patricia Klobusiakova
Patricia Klobusiakova 2016년 10월 29일
댓글: Patricia Klobusiakova 2016년 10월 30일
Hi,
I have 200 3D matrices with same dimensions. I'd like to create one 3d matrix that would contain mean of my 200 matrices by elements → (1,1,1) mean of 200 positions in my 3D matrices and so on ...
Here's my code:
for tt = 1:200;
M = ... (tt) loads the 3D data
end
mean()
Can you help me please?
Thanks.
  댓글 수: 3
Patricia Klobusiakova
Patricia Klobusiakova 2016년 10월 30일
Each number in one 3D matrix represents one voxel. I'd like to take these 200 matrices and calculate the mean of each voxel in order to create one 3D matrix that could represent these 200. I was thinking about using concatenate function to create 4th dimension→create one huge 4D matrix from these 200 3D (although I cannot imagine such a matrix) and then calculate mean along 4th dimension, but I don't know how to do that when I have a for loop there and matrix M changes with every iteration.
Patricia Klobusiakova
Patricia Klobusiakova 2016년 10월 30일
for ii = 1:nsub
for tt = 1:200;
M = spm_read_vols(spm_vol(files(tt).name(ii,:)));
end
mean(M,4)

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

채택된 답변

dpb
dpb 2016년 10월 30일
Well, you could do that (store in a 4D array, that is) but that requires memory for all 200 at once and there's no need when computing a mean to have all the data at once...just accumulate the totals and then divide after the loop is finished...
M = spm_read_vols(spm_vol(files(1).name)); % read the first into accumulator array
for tt = 2:200
M=M+spm_read_vols(spm_vol(files(tt).name)); % accumulate the total
end
M=M/200; % average
  댓글 수: 1
Patricia Klobusiakova
Patricia Klobusiakova 2016년 10월 30일
That's cool. I didn't think of that. Thank you very much!

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

추가 답변 (1개)

Kwin
Kwin 2016년 10월 30일
If you have your matrices all stored in one (four dimensional) matrix, for example M(:,:,:,k) would return the kth 3D matrix, but another order of the index would be fine as well, such as M(k,:,:,:), then, as dpb already mentioned, you can get the mean by simply using:
mean(M,location_of_k)
so for example for M(:,:,:,k), location_of_k=4 and for M(k,:,:,:), location_of_k=1.
  댓글 수: 1
Patricia Klobusiakova
Patricia Klobusiakova 2016년 10월 30일
Thanks for your answer! Could you take a look on the comment I wrote under my question? The problem is I am not able to create a 4D matrix because of a for loop I have in the code.

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by