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 visualizaciones (últimos 30 días)
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);

Respuesta aceptada

Voss
Voss el 2 de Mzo. de 2023
Editada: Voss el 2 de Mzo. de 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);
Columns 1 through 19 0.1427 0.1909 0.6562 0.8677 0.5249 0.5001 0.5126 0.4950 0.4015 0.7480 0.6064 0.2965 0.4470 0.8540 0.0620 0.7962 0.0655 0.4210 0.5483 0.7320 0.1553 0.4528 0.9172 0.7943 0.4984 0.0920 0.5065 0.7670 0.8833 0.9413 0.7070 0.9081 0.8201 0.2676 0.2575 0.9572 0.2423 0.5219 0.3583 0.9180 0.0653 0.6659 0.6691 0.5958 0.0771 0.3895 0.3259 0.7552 0.1235 0.7552 0.0255 0.5133 0.0838 0.9204 0.7070 0.0415 0.2399 Columns 20 through 38 0.8091 0.8324 0.9735 0.7625 0.1639 0.0137 0.5246 0.4360 0.1754 0.5830 0.6037 0.7346 0.8412 0.5184 0.9988 0.8760 0.4937 0.2619 0.9185 0.3342 0.0323 0.9154 0.6843 0.5786 0.3842 0.9591 0.8368 0.1709 0.7891 0.3301 0.9159 0.0464 0.0601 0.7611 0.7539 0.5120 0.7278 0.5848 0.4920 0.5417 0.1672 0.8984 0.5200 0.7526 0.1059 0.1004 0.8803 0.2670 0.1957 0.5120 0.1007 0.3480 0.8405 0.5090 0.8894 0.4093 0.1431 Columns 39 through 57 0.2618 0.5571 0.8303 0.3652 0.7710 0.5562 0.2191 0.4117 0.6547 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.4742 0.9229 0.9511 0.8459 0.2976 0.4156 0.4330 0.7915 0.4047 0.3121 0.3790 0.2918 0.0253 0.7953 0.5918 0.8061 0.6460 0.1004 0.4503 0.7675 0.2222 0.4206 0.7943 0.5298 0.6040 0.8725 0.9291 0.2157 0.8000 0.4759 0.4000 0.7737 0.6820 0.1131 0.0327 0.1915 0.0792 0.0077 Columns 58 through 76 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.9355 0.1498 0.9912 0.6019 0.9614 0.6260 0.5599 0.7205 0.8247 0.6861 0.3026 0.8810 0.2659 0.8608 0.3857 0.0769 0.9168 0.4857 0.4562 0.6330 0.2744 0.2874 0.1594 0.2741 0.4156 0.9193 0.1708 0.9804 0.5308 0.4886 0.2354 0.7723 0.9535 0.6051 0.3572 0.3569 0.1218 0.4919 Columns 77 through 95 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.1264 0.5181 0.6564 0.9865 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 0.8938 0.7682 0.8717 0.5886 0.2757 0.7473 0.1987 0.0763 0.2584 0.9651 0.0983 0.6667 0.9683 0.6575 0.4010 0.6357 0.6975 0.2726 0.9250 Columns 96 through 97 NaN NaN NaN NaN 0.6214 0.2073

Más respuestas (0)

Categorías

Más información sobre Multidimensional Arrays en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by