# creating uber matrix composed of smaller EEG data matrices (in a for loop): undesired outcome: creates 1 number that repeats in every cell for each subject

1 view (last 30 days)
Joanne Hall on 14 May 2022
Commented: Voss on 15 May 2022
Dear matlab,
Goal: to create an uber mother matrix of EEG channel data sets, into one matrix.
My EEG data for each individual subject is saved with the variable name " tf " (which time-frequency decomposition result), and is a 4-D double matrix: channels X frequencies X time points X trials.
The below for loop should load files into one uber mother matrix, which results in a 5-D matrix:
subjects X channels X frequencies X time points X trials
The uber matrix is created in 5-D, but there is a problem: The problem is that, in the end, there is only one number that repeats in every cell of the matrix, for each participant.
For the 1st subject, matlab takes the 1st cell and repeats the same number in all cells, for the rest of the matrix, for that subject.
For the 2nd sbject, matlab takes the single value from the 2nd row, 1st column, and repeats THAT number across the remaining cells, for that subject.
Below is code.
***********************************
clear, close all
% Get a list of data files ready to be analyzed
sublist = dir('*tf*.mat');
sublist = {sublist.name};
for subno=1:length(sublist)
% initialize matrices on 1st subject
if subno==1
tf_all = zeros([ length(sublist) size(tf) ]);
end
% place single-subject data into group-size matrices
tf_all(subno,:,:,:,:) = tf(subno);
end % end loop around subjects

Voss on 14 May 2022
Try changing this:
tf_all(subno,:,:,:,:) = tf(subno);
to this:
tf_all(subno,:,:,:,:) = tf;
##### 2 CommentsShowHide 1 older comment
Voss on 15 May 2022
You're welcome!
The problem was that tf(subno), which is one element of tf (specifically, element # subno), was being assigned to all elements of tf_all(subno,:,:,:,:).
Consider a similar situation with smaller matrices:
M = [1 2; 3 4];
M_all = zeros(2,2,2);
The way it was, was like saying this:
M_all(1,:,:) = M(1)
M_all =
M_all(:,:,1) = 1 1 0 0 M_all(:,:,2) = 1 1 0 0
when you meant to say this:
M_all(1,:,:) = M
M_all =
M_all(:,:,1) = 1 3 0 0 M_all(:,:,2) = 2 4 0 0
Anyway, let me know if you have any other questions about it. Otherwise, if that solves the problem, please click "Accept This Answer". I appreciate it!