Separating values in cell arrays
2 次查看(过去 30 天)
显示 更早的评论
Suppose, I have a cell array as follow:
a{1}=[2 3]
I would like to separate the values into two distinct values but at the same time, I want to add a value (in this case ‘1’) to a new column. The output should reflect something like this.
output{1}=[1 2]
output{2}=[1 3]
How can I go about this besides using for loop?
0 个评论
采纳的回答
更多回答(1 个)
Andrei Bobrov
2013-9-4
a = arrayfun(@(x)randi(20,1,randi([2 4])),1:5,'un',0); % Let your data
n = cellfun('length',a);
x = zeros(sum(n),2);
x(cumsum(n)-n+1,1) = 1;
x = cumsum(x);
x(:,2) = [a{:}]';
output = num2cell(x,2);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Array Geometries and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!