how can i combine contents of two cell arrays into one cell array of same length
2 次查看(过去 30 天)
显示 更早的评论
I have two cell arrays
Bill = {[1 2 3 4 5];[2 4 6 8 10]};
Ted = {[6 7 8 9 10];[12 14 16 18 20]};
and I want to merge them together so that
Excell = {[1 2 3 4 5 6 7 8 9 10]; [2 4 6 8 10 12 14 16 18 20]};
the only thing I can think of to combine them is
Excell = cellfun(@(v,w) [v,w],Bill,Ted,'UniformOutput',0)
but I imagine that there is a better more compact way to do this
thanks
0 个评论
采纳的回答
Geoff Hayes
2014-11-14
Joshua - I think that your solution works well. An alternative is the following
Excell = mat2cell(cell2mat([Bill , Ted]),[1 1]);
where we just combine the two cell arrays as [Bill , Ted] and then convert it to a 2x10 matrix with cell2mat. We then convert back to a cell array as mat2cell(...,[1 1]) where we use the [1 1] to indicate the row dimensions of the new cell array.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Cell Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!