Mean of elements across multiple matrices

Hi all, I have 50 matrices that are 1000 x 1000. I want to select a sub-area e.g. 300x300 (let's say in the middle), and find the mean of each element across all matrices. Generate a 300x300 matrix which consists of these mean elements. Also, I want to repeat this process for a number of other folders which contains same number of matrices. Many thanks, Andy.

 Accepted Answer

Stephen23
Stephen23 on 12 Sep 2018
Edited: Stephen23 on 12 Sep 2018
You don't given any details of the file format or how these matrices are stored, so I have used csvread to represent whatever file reading function matches your data. Change to suit your data.
D = 'directory where the files are stored';
S = dir(fullfile(D,'*.csv'));
N = {S.name}; % you might want NATSORTFILES from File Exchange.
C = cell(1,numel(N));
for k = 1:numel(N)
C{k} = csvread(fullfile(D,N{k}));
end
A = cat(3,C{:}); % concatenate all matrices into 3D array.
B = A(100:400,100:400,:); % get subarray using indexing.
M = mean(B,3) % mean of sub array along third dimension.
Repeat for each folder. See also:

3 Comments

Hi Stephen, Many thanks for your rapid and well-build answer. Sorry for not mentioning the file format. I am working with .im7 files that are read using readimx (and ad-on to MATLAB). After for loop I have added the following line to extract required matrices from each file:
XM = C.Frames{1,1}.Components{1,1}.Planes{1,1}
But this, of course, brings an error "Dot indexing is not supported for variables of this type". Before I had these two lines:
matrix_data = readimx([matrix_path matrix_data_files(j).name])
any_matrix = matrix_data.Frames{1,1}.Components{1,1}.Planes{1,1}
This means that it requires to import/read matrices across all im7 files.
@Andrius Patapas: given that C is clearly a cell array, what do you expect this to do?:
XM = C.Frames{1,1}.Components{1,1}.Planes{1,1}
You cannot access a cell array as if it were a structure. It seems that your image importing tool returns a structure (but I have no way to check this as you gave no link, explanation, or specification). This means you could either:
  1. inside the loop extract just the numeric data that you want (much simpler).
  2. inside the loop collect the entire structures. After the loop select the numeric data (works best with non-scalar structures, which apparently the ad-on designers did not use).
For example, inside the loop:
for ...
...
MD = readimx(fullfile(D,S(j).name))
C{k} = MD.Frames{1,1}.Components{1,1}.Planes{1,1};
end
Note: do NOT use concatenation for joining directories and filenames: fullfile is much better.
Ugh, I just noticed those curly braces: why does this ad-on use nested structures inside cells arrays? Most likely non-scalar structures would do the job perfectly, and provide simpler data access.
Andy Pat
Andy Pat on 12 Sep 2018
Edited: Andy Pat on 12 Sep 2018
@Stephen Cobeldick: It worked just fine - many thanks for your help and advice. Small correction to your code that I have identified for calculating mean: M = mean(B,3)
I will attempt using the non-scalar structures.

Sign in to comment.

More Answers (0)

Tags

Asked:

on 12 Sep 2018

Edited:

on 12 Sep 2018

Community Treasure Hunt

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

Start Hunting!