Addition in 3D matrix

2 次查看(过去 30 天)
Hello,
How can I sum in 3D using intervals;
example:
(62x50x341) 3d matrix
I want to add the following intervals in 3D:
Intervals; 8,30,31,31,30,31,30,31,31,28,31,29
is it possible to do it in a loop?
Thanks in advance

采纳的回答

the cyclist
the cyclist 2021-2-14
% Define some pretend data, the size of your original matrix
M = rand(62,50,341);
% Intervals
interval = [8,30,31,31,30,31,30,31,31,28,31,29];
% Get the sizes of M and intervals
[r,c,~] = size(M);
ni = length(interval);
% Calculate endpoints
endpoints = cumsum(interval);
% Calculate starting points
startpoints = [1 endpoints(1:end-1)+1];
% Preallocate an array for the summed intervals
output = zeros(r,c,ni); % Could also get these values from the sizes of the inputs
% Fill the output with the summed intervals
for ii = 1:ni
output(:,:,ii) = sum(M(:,:,startpoints(ii):endpoints(ii)),3);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by