Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
What is the proper way (in Prime 3.1) to create an if statement that compares the values of a vector to a single value? Please see the attached photo for a better description.
Solved! Go to Solution.
It depends on what you want as a result:
If you want a vector of 1's and 0's that's the same size as your comparison vector, use the "vectorization" operator.
If you want a single 1 or 0 (e.g. if all values are less than 0.2), use the comparison max(Vua/Vn)<0.2
Thank you Mark, I'm looking for the first result. I tried multiple arrangements of the "vectorize" operator and each resulted in an error. What is the correct way to use the vectorize operator in this situation?
Hmm.. it appears the vectorization operator does not seem to work with "if" statements. We could write a program. However, if you true and false results are simply 1 and 0, you don't even need the "if" statement:
Mathcad already stores "true" as 1 and "false" as 0.
Here's a solution using programming in case you want to use values other than 1 and 0:
Yes, some built-in functions like "max", "min" and obviously also "if" cannot be vectorized directly.
If you are happy with the "boolean" values 0 and 1, do as Mark has shown, simply type "Vec < 0.2".
Otherwise, if you also want different values to be returned, you may resort to a user written wrapper function which then can be vectorized:
WE
Here is a more versatile wrapper function (sort of "why didn't this come to my mind in first place") which can be used the same way as the built-in if-function and additionaly can be used vectorized, too.
Thank you very much Mark and Werner; these are all great ways to get around the vectorize limitations of the if statement.
In the attached program and picture I am attempting to put this to use. The program is attempting to compare values in a "vector divided by a scalar" to another "vector divided by a scalar". The program "strength" shows the 4 conditions and their outcomes, it obviously doesn't work. A few questions;
1. Is it possible to stack results and omit NaN results (as I attempt to do in the st program)?
2. Is it possible to nest if statements in a vectorize function, and if not:
3. Is there a way to iteratively compare the individual lines of a "vector divided by a scalar" to another "vector divided by a scalar" and place the results in a new vector?
1.) Yes. Your "st" is a good start; however, note that if it did work, the order of the vector's elements would not match the order of the original vectors.
To get your function to work, you would need to:
- Fix ORIGIN. It appears ORIGIN must be set to 0, but your program assumes ORIGIN is 1.
- Instead of comparing the value to NaN, you should use "IsNaN".
Regardless, I think "filterNaN" accomplishes what your "st" attempts. Though, it would still be in the wrong order (if order matters to you).
2.) I would think so. What does the error message say?
3.) Your program "strength" is a good start. You'd just need to put the whole thing inside a loop that iterates each element of the vector.
Two additional suggestions:
A.) Use "Return":
If (cond1) and (cond2)
Return "OK"
Else if ...
Return ...
B.) Add an "Otherwise" statement at the end (should never occur since one of the four should always be true, but I think it's good practice).
Here's some evidence for my comments on #1 & #2 (similar to Luc's example below):
Thank you very much Mark. I'm following all of your recommendations. In the attached image or program where/how would you add the returns to make this iterative for each value in Vua or Nua and return a vector with the results?
Actually, since those ifs are within the loop, you shouldn't use "return." Ignore that previous suggestion. "Return" causes the program to halt and will return that single value.
I think your program will work if you just move the j = j+1 to the last line of the loop (it is currently outside the loop).
It should look like:
(edited in paint since I can't open your file)
Thank you Mark. Moving it inside the for loop causes the error "This value must be a scalar or matrix". Is there anyway I can save such that you can open it? I can also redo it in Mathcad 15.
You need the array subscript "i" in your final "else if"
The function started working, I think this did the trick, thank you!
Like this?
Success!
Luc