How to join matrices of different lengths into one mother matrix

2 vues (au cours des 30 derniers jours)
Joanne Hall
Joanne Hall le 5 Fév 2023
Commenté : Paul le 5 Fév 2023
Dear Matlab,
I have an EEG signal dataset with 14 files.
GOAL: to join all files together into one mother uber matrix, i.e. similar to 'concatenate', so I can run stats.
The problem is (I think) that the files have different lengths. Some files are 5 minutes long, others are 10 minutes, etc. They have the same number of channels (i.e. 'rows'), but the datapoints, or 'columns' are different. How can I join them together? Please help. Thank you very much in advance. (Also I don't want to truncate to the shortest length, because I lose data).
  • Below is the code I have. (tried attahcing sample datafiles but too large)
************************************
close all; clear; clc;
% Get a list of data files ready to be analyzed
sublist = dir('*.mat');
sublist = {sublist.name};
% load in level-1 data
for subno=1:length(sublist)
load(sublist{subno})
% initialize matrices on 1st subject
if subno==1
GROUPsig = zeros([ length(sublist) size(dataFCP_pow2) ]);
end % end initialization loop
GROUPsig(subno,:,:) = dataFCP_pow2;
end % end loop around subjects
  4 commentaires
Image Analyst
Image Analyst le 5 Fév 2023
Can you crop down 2 or 3 files and attach them? But I agree with Paul below. It sounds like what you are suggesting would just complicate things for no reason. What do you plan on doing with this 3-D matrix once you've created it?
Joanne Hall
Joanne Hall le 5 Fév 2023
Thank you also for your reply! See below resp. to Paul. I tried to attach a sample zip file, but its too large. I'll keep trying

Connectez-vous pour commenter.

Réponse acceptée

Paul
Paul le 5 Fév 2023
Hi Joanne,
If you want to store different size arrays in a single data structure you probably should consider using a container object like a struct or cell array. Here's an example using a cell array containing two matrices, each with two rows.
GROUPsig{1} = rand(2,3);
GROUPsig{2} = rand(2,4);
If you want to run the same function on each array, use cellfun. In this example, we compute the mean the rows
rowmeans = cellfun(@(x) mean(x,2),GROUPsig,'UniformOutput',false)
rowmeans = 1×2 cell array
{2×1 double} {2×1 double}
rowmeans is cell array where each cell contains a 2x1 vector containing the mean of each row in the matrix contained in the corresponding cell of GROUPsig
rowmeans{1}
ans = 2×1
0.9552 0.4208
  7 commentaires
Walter Roberson
Walter Roberson le 5 Fév 2023
Oh, right, I forgot the pwelch() part of the call! I must have mis-copied on my phone!
Paul
Paul le 5 Fév 2023
You're very welcome.
One of the most basic things about cell arrays to remember is that to process the object contained in the cell you need access it with brace {} indexing. Parentheses () indexing works on the cell array itself, not the objects contained in the cells. And, as we've seen, cellfun() can be a handy function.
Also, check out this post on comma separated lists by @Stephen23 for more ways to use cell arrays, particularly if you want to use the objects contained in a cell as input arguments to a function or if you want to have the outputs of a function mapped into elements of a cell array.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by