MATLAB: How can i find the maximum value of the first element in a stack of matrices

for loopfunctionmatrices

What i would like to do is compare multiple matrices (up to ten), and find the maximum value in the first element of the matrices. Then the maximum value will be replaced, so i will get a new matrix consisting of only max values.
I would like to do this in a for loop since the matrices will change after each experiment i do.
so far i have done the following, where i have three(A,B and C) matrices that are 4×4:
c=zeros(4,4,3) # preallok
c(1:4,1:4,1)=A
c(1:4,1:4,2)=B
c(1:4,1:4,3)=C
I now have a 3x3x4 stack
MaxVal=max(c,[],3)
This does what i want. I am fairly new at Matlab and i can't do this in a for loop. Please help, thank you

Best Answer

You don't say or show how A, B, C were obtained, but therein is the heart of your problem -- do not create sequentially-named/numbered variables; as you've just learned, once you go that route then it becomes inordinately difficult to deal with them.
Instead as your solution shows, use arrays or other higher-level data constructs; in your example you can more efficiently write simply
c=[A;B;C];
but that still has the issue of having the three variables to begin with.
The answer is to preallocate as you have with the number of planes that will be in the loop and then populate the 3D array initially instead of using another variable.
nE=7; % example number of experiments
N=4; % size of arrays per experiment
Z=zeros(N,N,nE); % preallocate
for i=1:nE % get data for each experimental run


Z(:,:,i)=acquireData(i); % Get Z for the ith experiment here

end
ZMax=max(Z,[],3); % and get the max, keeping all data

The alternative logic path would be to only collect and keep the last set of data and the ZMax array and do the comparison/selection on each pass through the loop acquiring the new set of data.
This has the advantage of minimizing the amount of memory needed but presumes that once have the maximum for the experiments there's no further need for the individual measurements themselves.
ADDENDUM
Based on the additional information, the idea is more like
d=dir('SuitableWildCardFileName*.xls'); % get the list of files

nE=length(d); % # experiments found

for i=1:nE % get data for each experimental run
Z(:,:,i)=xlsread(d(i).name); % Get Z for the ith experiment here
end
ZMax=max(Z,[],3); % and get the max, keeping all data
This suffers the problem that it doesn't preallocate Z but for a 4x4 array of only 8-10 elements the dynamic reallocation will be unnoticeable additional delay over the inherent overhead of xlsread itself. One could, if the size is known a priori, go back to the previous example and hardcode in N; or if that can vary, then read the first file outside the loop and determine N from it to preallocate and then iterate from 2:nE instead of from 1.
Alternatively, there's still the option of simply just keeping ZMax if that's the only data needed.
d=dir('SuitableWildCardFileName*.xls'); % get the list of files
Zmax=[]; % create empty to reference
nE=length(d); % # experiments found
for i=1:nE % get data for each experimental run
Zmax=max([Zmax;xlsread(d(i).name)],[],3); % keep only the max values
end