MATLAB - 从向量中删除前导零和尾随零 [英] MATLAB - Remove Leading and Trailing Zeros From a Vector

查看:32
本文介绍了MATLAB - 从向量中删除前导零和尾随零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有前导零和尾随零的小波函数.我想去除小波之前或之后出现的所有零点.但是,我不想删除小波本身中的任何零.为简化起见,假设我有以下 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆