Skip to main content
1-Visitor
December 18, 2023
Question

Compare a vector with a number

  • December 18, 2023
  • 1 reply
  • 995 views

I have a vector of values and I would like to compare all these values inluced in the table in order to know if it is higher than 1. 

I was thinking to use "if" but I don't know how to use it. 

1 reply

25-Diamond I
December 18, 2023

Use A > 1 and you get a vector of 0's and 1's where a 1 (= TRUE) denotes an entry which is greater than 1.

Werner_E_0-1702920991985.png

If you insist that you want to see "OK" (and you have to provide a text/return value if the entry is smaller or equal to 1), you may define a function which expects a single scalar and returns those texts and call that function vectorized(!) with your vector A as argument.

Werner_E_1-1702921125673.png

I used the if-function when I defined the function "compare", but you may as well use the programmed if-statement instead

Werner_E_2-1702921207327.png

The reason we need to define a function is that Prime does not allow to vectorize the comparison operator, but it allows to vectorize the call of a function which uses that operator 😉

 

 

 

 

21-Topaz II
December 19, 2023

Hi,

Once you have the truth table of zeros and ones it is possible to trim out the zeros like this.

Capture.JPG