In a cell array (which contains num. vectors of different lengths [ex.1x159871]), pad the ends of each cell with 'NaN' according to the longest row [ex. 1x249359]
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear matlab,
I have a cell array with 17 sets of EEG signal data 'housed' in one mother cell array (ex. one cell is 1x159871 single).
Because they all have different column lengths, I want to pad the ends of each cell with 'NaN' according to the longest signal, so that in the end, I can create/convert the cell array into a mother uber matrix (which, of coarse, requires that all data have the same dimensions).
As I'm unfamiliar with how to perform operations of cell array data, any help or guidance would be greatly appreciated!
Thanks as always!
Joanne
ps., below is a sample code, I found from Fabio Freschi in 2019 (I just couldn't figure out how to reverse the dimensions in the first line of code to make it, for example, 1x47, instead of 47x1 and then to successfully execute the 2nd line of code without error popup):
% the data
A = {rand(47,1),rand(80,1),rand(97,1)};
% pad with NaNs
A = cellfun(@(x)[x(1:end); NaN(97-length(x),1)],A,'UniformOutput',false);
% make a matrix
A = cell2mat(A);
0 commentaires
Réponse acceptée
Voss
le 2 Mar 2023
Modifié(e) : Voss
le 2 Mar 2023
Here's that same example, but each cell contains a row vector:
% the data
A = {rand(1,47),rand(1,80),rand(1,97)};
% pad with NaNs
N = max(cellfun(@numel,A)); % making a variable instead of hard-coding 97
A = cellfun(@(x)[x, NaN(1,N-length(x))],A,'UniformOutput',false); % concatenate horizontally (,) instead of vertically (;)
% make a matrix
A = cell2mat(A(:)); % use (:) to make A a column; thus each cell's content becomes a row in matrix A
disp(A);
2 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Language Fundamentals dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!