Create columns with no zero sequentially elements of a vector
显示 更早的评论
I have a vector A=[4,2 3 2 2 0 0 1 2 2 0 0 1 2 3] and I am trying to create columns in new matrix with no zero sequentially elements, with a loop without success.Β1=[4,2 3 2 2] B2=[1 2 2] etc
I want to have a column with elements of A and when I find zero to create another column. Do you believe that it is feasible?
1 个评论
John D'Errico
2015-4-28
You do realize that matrices MUST be rectangular? So all columns of a matrix must be the same length. This is why you failed to produce a result, since your result would not have that property.
采纳的回答
更多回答(1 个)
Andrei Bobrov
2015-4-27
编辑:Andrei Bobrov
2015-4-27
B = zeros(size(A));
t = A > 0;
B(strfind(0,t],[0,1])) = 1;
b0 = cumsum(B);
Bout = accumarray(b0(t)',A(t)',[],@(x){x'});
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Algebra 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!