Basic operations with matrices
3 次查看(过去 30 天)
显示 更早的评论
I have 429 .mat numerical matrices which I need to import into the workspace.
Each of them has an identical structure (107 rows by 36 columns) and is sequentially named as 'subj_00000.mat' ... 'subj_00428.mat'.
After importing, I need to average all of them, to generate another matrix, which will also have a dimension of 107x36.
Finally, I need to linearly correlate each column of the average matrix with each column of each of the original 429 matrices, to generate a new matrix of 429 rows and 36 columns.
Any help on how to do this is highly appreciated.
0 个评论
采纳的回答
Stephen23
2018-12-7
编辑:Stephen23
2018-12-9
Assuming that each .mat file contains only one variable and there are no other similarly named .mat files in the same directory:
D = 'path to where the .mat files are saved';
S = dir(fullfile(D,'subj*.mat'));
N = numel(S);
C = cell(1,N); % preallocate cell array
for k = 1:N
T = load(fullfile(D,S(k).name));
C(k) = struct2cell(T);
end
A = cat(3,C{:}); % join all of your matrices into a 107x36x429 array
M = mean(A,3)
The linear correlation I leave up to you...
3 个评论
Stephen23
2018-12-9
编辑:Stephen23
2018-12-9
Now the only problem is that the last line M = mean(A,3) returns a 107 x 36 matrix, which puzzles me since the third dimesion of the matrix A is a 429 array"
That is exactly what we would expect to happen: the dimension which is averaged over (in this case the third dimension) will only have size 1. Consider this small example:
>> A = [1,2;3,8]
A =
1 2
3 8
>> mean(A,1)
ans =
2 5
"My final matrix needs to be 429 x 36. How can I fix this? "
Your original question states " I need to average all of them, to generate another matrix, which will also have a dimension of 107x36", which is what I showed in my answer.
If you really to want to average over the first dimension then note that array A will have size 107x36x429, so calculate the mean over the first dimension:
M = mean(A,1);
M = permute(M,[3,2,1])
更多回答(1 个)
Cris LaPierre
2018-12-7
Your username caught my attention so thought I'd help you out a little.
My recommendation would be to build a 107 x 36 x 429 array. Each "slice" would be a different subject. For example, the matrix for subj_00000 would be at (:,:,1) and for subj_00428 would be at (:,:,429).
Taking the average is now trivial. Use the M = mean(A,dim) syntax of mean to average in the 3rd dimension, resulting in a 107x36 matrix.
You may find MATLAB Onramp helpful if you are just getting started with MATLAB. For what you ask here, chapter 5 may be helpful.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!