Skip to main content
8-Gravel
June 19, 2023
Solved

Return only the KP value when F is greater than 0

  • June 19, 2023
  • 2 replies
  • 1906 views

Dear All,

 

I am trying to write a program to return the only KP values when F is greater than 0.

 

davidlimtw_1-1687172716513.png

 

 

Best answer by Werner_E

Again its not really clear what exactly you expect as result and giving one or two examples would have helped.

 

In the following approach I assumed that you want a vector with the values of KP but replace the original value if the value in F at the corresponding position is negative.

Here is a short approach using vectorization

Werner_E_0-1687177203159.png

We can use a table built with augment to check the results

Werner_E_1-1687177248295.png

 

And here is a more "natural" approach with a function using a for loop

Werner_E_4-1687178081241.png

You can replace the 0m by NaN or whatever may be appropriate.

We can check if both ways return the same result

Werner_E_5-1687178123308.png

 

Using the if-function we can write it less space consuming

Werner_E_6-1687178181637.png

One may think that we could simply vectorize that if-function

Werner_E_8-1687178266989.png

but this does not work

Werner_E_9-1687178290269.png

because Prime does not allow to vectorize comparison operators like the < directly, but we are allowed to vectorize a function which uses the comparison:

Werner_E_10-1687178360095.png

Werner_E_11-1687178371793.png

 

Prime 9 worksheet attached

 

 

2 replies

8-Gravel
June 19, 2023

I would like to add also a function to filter to return only the KP where F > 0 and another matrix showing the only F > 0 values.

25-Diamond I
June 19, 2023

@davidlimtw wrote:

I would like to add also a function to filter to return only the KP where F > 0 and another matrix showing the only F > 0 values.


Examples always would be welcome to clarify what exactly is needed.

A I understood it now you want the values in both vectors removed when F is zero or negative so only the positive values in F remain and also the corresponding values in KP?

Could be done in many ways, one is shown here

Werner_E_0-1687179913577.png

 

Werner_E25-Diamond IAnswer
25-Diamond I
June 19, 2023

Again its not really clear what exactly you expect as result and giving one or two examples would have helped.

 

In the following approach I assumed that you want a vector with the values of KP but replace the original value if the value in F at the corresponding position is negative.

Here is a short approach using vectorization

Werner_E_0-1687177203159.png

We can use a table built with augment to check the results

Werner_E_1-1687177248295.png

 

And here is a more "natural" approach with a function using a for loop

Werner_E_4-1687178081241.png

You can replace the 0m by NaN or whatever may be appropriate.

We can check if both ways return the same result

Werner_E_5-1687178123308.png

 

Using the if-function we can write it less space consuming

Werner_E_6-1687178181637.png

One may think that we could simply vectorize that if-function

Werner_E_8-1687178266989.png

but this does not work

Werner_E_9-1687178290269.png

because Prime does not allow to vectorize comparison operators like the < directly, but we are allowed to vectorize a function which uses the comparison:

Werner_E_10-1687178360095.png

Werner_E_11-1687178371793.png

 

Prime 9 worksheet attached