how to remove unwanted zeros from a vector
Show older comments
I have a column vector of the type [ 0 0 0 0 4 5 6 0 0 0 9 9 8 7 6 0 0 0 0 0 0 3 4 4 0 0 0]
Want to remove unwanted zeros to get the form [ 0 4 5 6 0 9 9 8 7 6 0 3 4 4 0]
i -e I don't wana remove all zeros or zeros in the beginning and end. find will not work I guess:
Accepted Answer
More Answers (2)
Azzi Abdelmalek
on 22 Jun 2016
a=[ 0 5 0 0 0 0 4 5 6 0 0 0 9 9 8 0 7 6 0 0 0 0 0 0 3 4 4 0]
idx=a==0
ii1=strfind([0 idx],[0 1])
ii2=strfind([idx 0],[1 0])
idy=[];
for k=1:numel(ii1)
idy=[idy ii1(k)+1:ii2(k)]
end
a(idy)=[]
Andrei Bobrov
on 23 Jun 2016
out = v(v | [1,v(1:end-1)])
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!