필터 지우기
필터 지우기

How to join matrices of different lengths into one mother matrix

조회 수: 2 (최근 30일)
Joanne Hall
Joanne Hall 2023년 2월 5일
댓글: Paul 2023년 2월 5일
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
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?
Joanne Hall
Joanne Hall 2023년 2월 5일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Paul
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 = 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
Walter Roberson
Walter Roberson 2023년 2월 5일
Oh, right, I forgot the pwelch() part of the call! I must have mis-copied on my phone!
Paul
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by