MATLAB Answers

Addition in 3D matrix

18 views (last 30 days)
How can I sum in 3D using intervals;
(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

Accepted Answer

the cyclist
the cyclist on 14 Feb 2021
% 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);
  1 Comment
Orlando Ramirez-Valle
Orlando Ramirez-Valle on 14 Feb 2021
It worked !!!, I really appreciate your help. Thank you

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!