How to join matrices of different lengths into one mother matrix

1 view (last 30 days)
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 Comments
Joanne Hall
Joanne Hall on 5 Feb 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

Sign in to comment.

Accepted Answer

Paul
Paul on 5 Feb 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 Comments
Paul
Paul on 5 Feb 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.

Sign in to comment.

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by