MATLAB-从向量中删除前导零和尾随零 [英] MATLAB - Remove Leading and Trailing Zeros From a Vector
问题描述
我有一个带有前零和后零的小波函数.我想去除在小波之前或之后出现的所有零.但是,我不想删除小波本身内的任何零.为简化起见,假设我具有以下1x11向量:
I have a wavelet function with leading and trailing zeros. I would like to strip all the zeros which occur before or after the wavelet. However, I would not like to remove any zeros within the wavelet itself. To simplify, let's say I have the following 1x11 vector:
0 0 0 -2 -1 0 -1 -2 0 0 0
在删除前导零和尾随零后,向量应为:
After removing leading and trailing zeros the vector should be:
-2 -1 0 -1 -2
我的实际向量很大,而性能是我最关心的问题.我是MATLAB的新手,不胜感激有关如何尽可能高效地完成此任务的任何提示.
My actual vectors are large and performance is my primary concern. I am a MATLAB novice and would appreciate any tips on how to accomplish this task as efficiently as possible.
推荐答案
尝试一下
y = x(find(x,1,'first'):find(x,1,'last'));
find(x,1,'option')
命令为您提供第一个和最后一个非零索引.
The find(x,1,'option')
command gives you first and last non-zero indices.
这篇关于MATLAB-从向量中删除前导零和尾随零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!