How to join matrices of different lengths into one mother matrix
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Image Analyst
2023-2-5
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?
采纳的回答
Paul
2023-2-5
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 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}
7 个评论
Walter Roberson
2023-2-5
Oh, right, I forgot the pwelch() part of the call! I must have mis-copied on my phone!
Paul
2023-2-5
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!