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

Negative function

MikeArmstrong
12-Amethyst

Negative function

In the attached sheet I have written a function which will find the location of a negative value in a vector.

It works, but when 2 more values are duplicated in the vector it only returns the location of the last found negative in the vector.

Any ideas?

Regards

Mike
11 REPLIES 11

On 12/5/2009 8:24:03 AM, Armo wrote:
== In the attached sheet I have written a function
which will find the location of a negative value in
a vector.
== It works, but when 2 more values are duplicated
in the vector it only returns the location of the
last found negative in the vector.

change 'location←...' to
'locationrows(location)←...'

This has the advantage that even a single element
will be returned as a vector, thus allowing a
consistent method of using the result (it always
needs indexing) and you can use 0 to distinguish
between no negatives found and one found in the
first element.

Stuart

Once again cheers for the help works fantastic.

Regards

Mike

Match is designed to return a list of indices for matching values. It can usefully be used here.
__________________
� � � � Tom Gutman

Tom

Cheers for the help, again works exactly as required.

I have attached a sheet with both suggestions for completeness and so other collab�s can see the progression.

Regards

Mike

I also have a very similar problem with the attached sheet but cannot incorporate the comments to make this function yield the location of the maximum value if one or more values are duplicated.
Regards

Mike

On 12/6/2009 7:05:47 AM, Armo wrote:
== I also have a very similar problem with the
attached sheet but cannot incorporate the comments
to make this function yield the location of the
maximum value if one or more values are duplicated

See attached.

Stuart

Once again thanks for your time.

Regards

Mike

You are setting location to a scalar. It can never have a value other than a scalar. In particular, it cannot represent multiple values.

Also, location will, at the end of the process, contain the last value assigned. In your code that is not the location of the maximum value, but rather the location of the last value that is greater than the first value. In your test case the maximum value is in the last location, and so you get that location. Not so in general.
__________________
� � � � Tom Gutman

Tom you are correct.

I found the problem in my sheet. As it was written, it would only return the location of the last element in the (vec) which is greater than the first element.

With the help from Stuart, I have a function which will keep track of the current maximum value as it iterates through vec.

Cheers for the help.

Regards

Mike

>>With the help from Stuart, I have a function which will keep track of the current maximum value as it iterates through vec.<<

No need for such iteration. Stuart already posted how to use match and max to get a vector of locations of the maximum value.
__________________
� � � � Tom Gutman

I know there was a simple way to get the required result, but as a challenge and to enhance my limited programming knowledge, I was trying to code the function the long winded way.

Regards

Mike
Announcements

Top Tags