Basic operations with matrices

3 views (last 30 days)
Connectome
Connectome on 7 Dec 2018
Edited: Stephen23 on 9 Dec 2018
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.

Accepted Answer

Stephen23
Stephen23 on 7 Dec 2018
Edited: Stephen23 on 9 Dec 2018
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 Comments
Stephen23
Stephen23 on 9 Dec 2018
Edited: Stephen23 on 9 Dec 2018
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])
Connectome
Connectome on 9 Dec 2018
Actually you were totally right on the first place, sorry I got confused.
It is correct that the average matrix M is still 107 x 36. Hence, I will use
M = mean(A,3)
The final 429 x 36 matrix will be the result of the linear correlation between each column array in M and each column array in the original individual matrices.
Thank you very much, my question has been completely resolved.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 7 Dec 2018
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.
MATLAB has functions for correlation. See if the corr function will meet your needs.
You may find MATLAB Onramp helpful if you are just getting started with MATLAB. For what you ask here, chapter 5 may be helpful.
  1 Comment
Connectome
Connectome on 8 Dec 2018
Edited: Connectome on 8 Dec 2018
Thanks for your prompt reply! I'll have a look to the functions you suggested and will go through the MATLAB Onramp

Sign in to comment.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!