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]
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2023-3-2
编辑:Voss
2023-3-2
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!