MATLAB Answers

delete zero elements from vector

5 views (last 30 days)
I have k (1,600) size, it contain zeros at 451-600 elements.
I want to delete these zero elements. At the end of this vector i want to add two elements with value equal to the last element .
thank you in advance


Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 18 Nov 2020
k = nonzeros(k);
k(end+1:end+2) = k(end)

  1 Comment

zina shadidi
zina shadidi on 18 Nov 2020
Thanks alot Stephen Cobeldick ;
its work with the vector. please let me ask you if I have an (n by m ) matrix how can i grow it ,
I have an array of (452x 6) and i want to make it grow by adding 2 column

Sign in to comment.

More Answers (1)

Bastian Arturo Romero Mardones
You can use:
k(k==0) = [];
k(length(k)+1:length(k)+2) = k(1,length(k));

  1 Comment

zina shadidi
zina shadidi on 18 Nov 2020
thank you very much Bastian Arturo Romero Mardones

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!