Question: Finding first and last none zero entries in Vector

Hi all,

Is there an easy way to find first and last entries of a vector which is non zero?

 

fa:=proc(x)
local i;
while x[i]<>1 do
i:=i+1;
end do;
end proc;

 

fa(<1|0|0|1|0|0>);

 

That does not work. I have also tried to use

while 'x'[i]<>1 do

 

keeps getting error saying: Error, (in fa) bad index into Vector

 

What's the best way to get result 1 (in this case) for the first none zero entry?

(and 4 for the last none zero entry)

 

Thanks!

 

Casper

Please Wait...