build a matrix from cell
3 次查看(过去 30 天)
显示 更早的评论
Dear
I have a cell array which I need to select only first column of each cell separately and put it into a matrix. But each column has different number of row.
can somebody help me ? I think I can build a matrix of zero with the size of my cell and then I put each column separately. However, following effort does not work.
[n,m]=size(X)
A = zeros(100,n);
for i=1:n
A (:,n) = X{n}(:,1)
end
0 个评论
回答(2 个)
Azzi Abdelmalek
2013-5-8
X={magic(4),eye(4),rand(4)} % Example
y=cell2mat(cellfun(@(x) x(:,1),X,'un',0))
3 个评论
Azzi Abdelmalek
2013-5-8
编辑:Azzi Abdelmalek
2013-5-8
X={magic(5);eye(4);rand(6)}
m=numel(X);
n=max(cellfun(@(x) size(x,1),X));
out=zeros(n,m);
for k=1:m
a=X{k}(:,1);
out(1:numel(a),k)=a;
end
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!