我有一个大小为1227的信息 其中的每个单元格都有一行数据。每行大小不同 我想创建一个1227行的矩阵 来自不同单元格的每一行
我该怎么做?
答案 0 :(得分:1)
在矩阵中,根据定义,所有行都具有相同的长度。您必须填写值,例如NaN
:
data = {1:3, 4:5, 6:9}; %// example data
M = numel(data); %// number of rows
N = max(cellfun(@numel, data)); %// maximum number of columns
matrix = NaN(M, N); %// initiallize with NaN (used as fill value)
for m = 1:M
row = data{m}; %// extract values of cell
matrix(m, 1:numel(row)) = row; %// write to first entries of matrix row
end
在此示例中,
>> celldisp(data)
data{1} =
1 2 3
data{2} =
4 5
data{3} =
6 7 8 9
>> matrix
matrix =
1 2 3 NaN
4 5 NaN NaN
6 7 8 9