Previous Up Next

21.2.2  Finding and removing leading/trailing zeros

You can find leading and/or trailing zeros in a vector and remove them by using the trim command (see Section 5.2.5, Section 28.1.9 and Section 28.2.2 for other usages of trim).

Example

v:=[0,0,1e-16,0,-1e-13,1,2,3,0,0,0]:;

To remove leading zeros from v:

trim(v,left)
     

1,2,3,0,0,0
          

To remove leading and trailing zeros with threshold set to 10−14 (the default is the value of epsilon(), which is 10−12 in this case):

trim(v,1e-14)
     

−1.0×10−13,1,2,3
          

To return the start and length of the nonzero part of v:

d,n:=trim(v,index)
     
5,3           

Now, enter

mid(v,d,n)

to obtain the trimmed variant of v.


Previous Up Next