Mean and SD for 3D matrix

2 views (last 30 days)
doehr001
doehr001 on 17 Feb 2021
Commented: Rik on 18 Feb 2021
Hello,
I have a 3D matrix (140 rows, 572 columns) for 91 cases which are safed in one .mat file. How can I create a new .mat file with the average Matrix and one for the SD Matrix? I have tried:
FileData = load('Matrix.mat');
Matrix_Avg = mean(3, Parameter);
But always get an error.
Help is much appreciated!
  2 Comments
Rik
Rik on 17 Feb 2021
From your other question I suspect you don't have a 3D array, but a struct array. Is that correct? Please attach your data or post code that creates similar data.
doehr001
doehr001 on 17 Feb 2021
Edited: Rik on 17 Feb 2021
Thank you for your reply! The data file is too big unfortunetly. The code is use in full is:
pfad = '/Users/...../';
liste = dir(fullfile(pfad,'*.xlsm'));
files = {liste.name};
xlRange = 'A2:UZ141'
for i=1:numel(files)
thisSheet = sprintf('Sheet1');
fullFileName = fullfile(pfad,files{i})
if exist(fullFileName, 'file')
% Variable name
Parameter{i} = xlsread (fullFileName, thisSheet, xlRange);
else
message = sprintf('File not found:\n%s', fullFileName);
uiwait(warndlg(message));
end
end
Matrix = cat(3,Parameter{:});
save( 'Matrix.mat', 'Matrix')
Matrix_Avg = mean(3, Matrix);
Matrix_SD = std(3,Matrix);
save('Matrix_Avg.mat', 'Matrix_Avg')
save('Matrix_SD.mat', 'Matrix_SD')

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 17 Feb 2021
The cat function is one of the few (if not the only) functions where the dimension input comes first. In most other functions (including both mean and std) it comes after the data.
A = randi(16, 4, 4);
B = magic(4);
C = cat(3, A, B) % Concatenate in dimension 3
C =
C(:,:,1) = 4 4 14 9 13 6 9 15 12 8 5 6 6 13 11 6 C(:,:,2) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
D = mean(C, 3) % Take the mean along dimension 3
D = 4×4
10.0000 3.0000 8.5000 11.0000 9.0000 8.5000 9.5000 11.5000 10.5000 7.5000 5.5000 9.0000 5.0000 13.5000 13.0000 3.5000
  6 Comments
doehr001
doehr001 on 17 Feb 2021
The error is:
Error in XLSRead_MatrixExport_Feb2021 (line 33)
MAvg = mean(Matrix, 3);
I have exported a view of the files so that I can upload a sample .mat file.
Rik
Rik on 18 Feb 2021
When posting an error message, please make sure to post all the red text. And please use the layout tools to format your code as code.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!