cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

find last value in a vector greater than 0

Otilia
10-Marble

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

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:Otilia)

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

View solution in original post

5 REPLIES 5
LucMeekes
23-Emerald III
(To:Otilia)

Easy:

LM_20190228_LastPositive.png

Success!

Luc

Werner_E
25-Diamond I
(To:Otilia)

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

LucMeekes
23-Emerald III
(To:Werner_E)

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

Werner_E
25-Diamond I
(To:LucMeekes)

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.

Otilia
10-Marble
(To:Werner_E)

Hello,

 

All solutions work for me, but when i checked my data after reading what both of you wrote, i realized i have some vectors with all values 0, which would return an error, so i ended up using the function from LastPositive4 which is working for me. 

Yesterday i was trying with 'match' before i posted here, but i didn't get it right. Thank you both very much.

 

Otilia

Announcements

Top Tags