How to have a sum of 2d fields in a 3d matrix so the time dimension remains?
2 次查看(过去 30 天)
显示 更早的评论
Hello,
I have the following problem:
I have a 3D matrix with dimensions seen below and I am trying to get mean values of fields on first two dimensions,
while keeping the last dimension resolution, so the final matrix would be a 31 length vector. I have tried as seen below,
but that failed for the moment.
I would greatly appreciate a suggestion on how to do that correctly
% ice_sum_1st_aug is a matrix 480x30x31 - trying to get sum of values on 480x30 bits against time
% the needed result would be a vector of 31 points in lenght
for k=1:31
icesum_1st_aug(k)=sum(ice_1st_august(:,:,k));
end
0 个评论
采纳的回答
Walter Roberson
2019-2-28
icesum_1st_aug = sum(sum(ice_1st_august,1),2);
If you have R2018b or later then
icesum_1st_aug = sum(ice_1st_august, [1 2]);
If you really want to loop,
nk = size(ice_1st_august,3);
icesum_1st_aug = zeros(1,1,nk);
for k = 1 : nk
icesum_1st_aug(1,1,k) = sum(reshape(ice_1st_august(:,:,k),[],1));
end
更多回答(1 个)
Steven Lord
2019-2-28
If you're using release R2018b or later, both sum and mean (your code used sum but your description of your goal indicated you're looking for the mean) accept a vector as the dimension input argument to operate over multiple dimensions simultaneously.
A = randi(10, [4, 5, 3]);
meanOfEachPage = mean(A, [1 2]);
You can check this, for example for the first page of A:
firstPage = A(:, :, 1);
meanOfFirstPage = mean(firstPage(:));
meanOfFirstPage - meanOfEachPage(1)
See the Release Notes for a list of the functions that support this functionality.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!