Skip to main content
1-Visitor
Feb 28, 2019 2:29 PM UTC
Solved

find last value in a vector greater than 0

Hello,

 

I have to find the last value from a vector which is bigger from 0, as in the attached example. How can it be determined?

 

Thank you,

Otilia

Werner_E
Werner_EAnswer
25-Diamond I
Feb 28, 2019 4:01 PM UTC

There are many ways to skin a cat.

Here are a few alternatives but I'd stay with Luc's approach, maybe adding some error check as shown in variant 3.

Variant 4 is a classic programming approach.

B.png

5 replies

23-Emerald V
Feb 28, 2019 2:35 PM UTC

Easy:

LM_20190228_LastPositive.png

Success!

Luc

Translate
Werner_E
Werner_E25-Diamond IAnswer
25-Diamond I
Feb 28, 2019 4:01 PM UTC

There are many ways to skin a cat.

Here are a few alternatives but I'd stay with Luc's approach, maybe adding some error check as shown in variant 3.

Variant 4 is a classic programming approach.

B.png

Translate
23-Emerald V
Feb 28, 2019 4:33 PM UTC

My implementation gives an error message when there is no positive element in V. And the error message will be at least somewhat to the point.

Your alternatives (LastPositive2, 3 and 4) will fail when ORIGIN is other than 0...

 

Luc

Translate
Werner_E
25-Diamond I
Feb 28, 2019 5:46 PM UTC

Correct, but you would simply have to replace "0" by "ORIGIN" to make the calculations ORIGIN-aware. But as already written above I'd prefer your solution anyway as its the shortest and most streamline.

I'd just rather prefer an error handling I can control rather than having the calculations fail with an error thrown by the built-in command.

It depends on how you would use the functions if  a custom made error message created with the error command is preferable or if the function should still return a well defined result like NaN in case of an error. I usually prefer a NaN result.

Translate