find the first and last nonzero element of an array

4 views (last 30 days)
exmple
a=[0,0,1,2,3,4,0,0]
which gives me positition 3, value 1 and position 6, value 4
Thanks!!

Answers (1)

Jan
Jan on 1 May 2013
Is this a homework question?
You are looking for a method to find the first and last element different from zero. Fortunately Matlab's find command can solve this directly. Therefore I assume, you did not search in the documentation already. While reading the Getting Started chapters is a good idea in general, you could search directly also:
docsearch find
Or ask Google about "Matlab find first non-zero".

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!