MATLAB: A sum of the many 3d arrays (just third dimension, and first and second stay steady)

3darrayarrayscellcell arraycell arraysMATLABmatrixmatrix arraysum

Hey all
I have a 408 3d single arrays. Each one is (720*360*12).
I want to write a code to sum the third dimension of all, without any change in the first and second ones.
So, in the end, I would like to have: 720*360*408 3d array.
As I am new to this topic, I would be appreciated if you could help me.

Best Answer

No, you do not need to build a 4D array.
projectdir = 'NameOfDirectory';
dinfo = dir(fullfile(projectdir, '*.mat'));
filenames = fullfile(projectdir, {dinfo.name});
numfiles = length(filenames);
total_values = [];
for K = 1 : numfiles
thisfile = filenames{K};
filedata_struct = load(thisfile);
fn = fieldnames(filedata_struct);
first_field = fn{1};
this_data = filedata_struct.(first_field);
if K == 1
total_values = this_data;
else
total_values = total_values + this_data;
end
end
The above code would have to be altered to suit the way that data is stored in your files.