Mean and SD for 3D matrix

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 件のコメント

Rik
Rik 2021 年 2 月 17 日
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 2021 年 2 月 17 日
編集済み: Rik 2021 年 2 月 17 日
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')

サインインしてコメントする。

回答 (1 件)

Steven Lord
Steven Lord 2021 年 2 月 17 日

0 投票

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 件のコメント

doehr001
doehr001 2021 年 2 月 17 日
Thank you Steven! When I use the:
Avg = mean(3, Matrix);
I always get an error. Now I am not clear why as there is no explanation other than error in line ...
The matrix I am loading is 140x572/ 91 doubles. Do you have any idea why I am getting this error?
Rik
Rik 2021 年 2 月 17 日
Did you read what Steven wrote? The syntax for the mean function is not what you wrote, it should be mean(data,dim).
doehr001
doehr001 2021 年 2 月 17 日
Yes, I did. I changed it to Avg = mean(Matrix, 3);
I still get the error.
Steven Lord
Steven Lord 2021 年 2 月 17 日
What is the full and exact text (everything displayed in red and/or orange) of the error/warning messages you received when you ran the following code?
Avg = mean(Matrix, 3);
doehr001
doehr001 2021 年 2 月 17 日
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 2021 年 2 月 18 日
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.

サインインしてコメントする。

カテゴリ

製品

リリース

R2020b

タグ

質問済み:

2021 年 2 月 17 日

コメント済み:

Rik
2021 年 2 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by