How to combine vectors of different length in a cell array into matrix in MATLAB

依然范特西╮ 提交于 2019-11-27 14:53:42
abcd

EDIT:

For a cell of row vectors as in your case, this will pad vectors with zeros to form a matrix

out=cell2mat(cellfun(@(x)cat(2,x,zeros(1,maxLength-length(x))),C,'UniformOutput',false))

out =

     1     2     3     0     0
     1     2     3     4     5
     1     2     3     4     0

A similar question was asked earlier today, and although the question was worded slightly differently, my answer basically does what you want.

Copying the relevant parts here, a cell of uneven column vectors can be zero padded into a matrix as:

out=cell2mat(cellfun(@(x)cat(1,x,zeros(maxLength-length(x),1)),C,'UniformOutput',false));

where maxLength is assumed to be known. In your case, you have row vectors, which is just a slight modification from this.

If maxLength is not known, you can get it as

maxLength=max(cellfun(@(x)numel(x),C));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!