Create columns with no zero sequentially elements of a vector
Mostrar comentarios más antiguos
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 comentario
John D'Errico
el 28 de Abr. de 2015
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.
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 27 de Abr. de 2015
Editada: Andrei Bobrov
el 27 de Abr. de 2015
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'});
1 comentario
nmartz
el 27 de Abr. de 2015
Categorías
Más información sobre Linear Algebra en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!