MATLAB - Remove Leading and Trailing Zeros From a Vector

只谈情不闲聊 提交于 2019-11-28 09:41:55

Try this

 y = x(find(x,1,'first'):find(x,1,'last'));

The find(x,1,'option') command gives you first and last non-zero indices.

i1 = find(X, 1, 'first')

will give you the index of the first non-zero element of X

i2 = find(X, 1, 'last') 

will give you the index of the last one. Then

X(i1:i2)

will give you the array with the leading and trailing zeros stripped.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!