Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Is there a way to find max values for two columns, outputted as one column in prime 9?
Solved! Go to Solution.
There are at least two ways to accomplish what you want, on worksheet level and by using a function. Both are shown in the attached worksheet.
Success!
Luc
There sure is a way to do what you are asking for, but its not clear to me what exactly you need.
Best you provide some samples in a worksheet, state what result you are expecting and attach the sheet here.
What I understood from your short question was, that you want to find the maximum of the first and the maximum of the second vector and want the two values returned in a 2 x 1 vector.
Could be done that way:
P9 worksheet attached
Hi Werner, thank you for you're reply.
I've attached my worksheet, with a workaround solution I created to solve this issue. The attached image is from the second page.
Thanks
There are at least two ways to accomplish what you want, on worksheet level and by using a function. Both are shown in the attached worksheet.
Success!
Luc
Thank you! Both methods works as intended.
From my understanding it seems like both methods compare rows in A with the highest value row in B (if B is a vector).
As a follow up question. If you where to compare two vectors, and compare row 1 in A with row 1 in B, row 2 in A with row 2 in B etc... Could you use a similar approach? I would assume the worksheet level approach would not allow for this.
You would have to define what should happen if the two vectors are not of equal length.
Here an approach which uses vectorization. But it only work if both vector have the same length
The function "largest" had to be introduced because the built in "max" can't be vectorized.
Here a method using ranges on worksheet level. It works If A has the same number of elements than B or fever. If A has more elements than B, then A would be truncated
And here a utility function is used. It works OK no matter which vector is larger. I have chosen the first option in my foregoing list (ignoring the extra elements in either vector).
The vectors where ment to have the same number of rows. Function "largest" is exactly what I was looking for!
The reason this is useful to me Is because I found Mathcad is not able to calculate the worksheet multiple times with changing variables. At least this way most of the expressions I typically use, could be vectorized and calculated continuously for every variable. Tough it would complicate the worksheet and increase the chance of me doing something incorrectly when the calculations get long.
I do however think it could be solved fairly easily with API, and something like MathcadPy in python to iterate a variable lets call it "n_row", that defines which row in a table the calculations uses, and which row in excel to export to. Previously I've done it with a combo box, and I've manually been changing "n_row" 1,2,3..... 😅
Being able to turn complete worksheet or part of it into a function of a couple of input variables is a long standing wish for Mathcad and also for Prime.
At the time all you could and should do is to turn every single calculation in your sheet into a function depending on those input variables. This may be cumbersome and ugly looking, though.
A simple example:
Lets say that the calculation c*(a+b) should be calculated and calculation is done with a+b as an intermediate result.
For a single set of the three input variables it would look like this:
Using the vector approach you seem to favor it looks like
Most versatile is to set up functions
which then can either be used with single input triples
or with input vectors (don't forget to vectorize)
Thank you. I have played around a bit with the vector approach before, and it have worked well for me in most cases as long as everything is a vector of the same length.
What I'm most worried about with this approach, is when I need to use a lot of functions or when there is already used vectors (like the example image) in the calculations. Now I won't have the chance to change everything in this sheet to the vector approach anyways because it's 20 pages of calculations for my thesis, so I'm already very busy.
If I get time in the future I want to try to make something simple with Python and MathcadPy as mentioned earlier. Hopefully if I manage to make an executable program for changing an input/output range, file path and so forth that would be the goal.
The only issue I see with this solution inside prime, is I haven't found a way of changing what column to export to inside Excel for every iteration. For my manual approach I currently did this, which I think is quite messy. The goal with Python is to also change the Excel position "Sheet1!A(n_row):N(n_row)".
Sorry for not attaching work sheet.
Yes, vector approach has limitations, especially when the calculations themselves must use vectors (not related with the input values vectors.
Here the approach of turning all calcs into functions can help, but it quickly gets quite ugly looking and hard to read for long winded calculation sheets.
Your approach using Python and the API looks promising.