cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Need help navigating or using the PTC Community? Contact the community team. X

Max value of columns/rows

TA_10206400
8-Gravel

Max value of columns/rows

Is there a way to find max values for two columns, outputted as one column in prime 9? 

 

 

 

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:TA_10206400)

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

View solution in original post

9 REPLIES 9
Werner_E
25-Diamond I
(To:TA_10206400)

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:

Werner_E_1-1682291502231.png

P9 worksheet attached

 

Skjermbilde 2023-04-24 kl. 20.53.17.pngHi 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

LucMeekes
23-Emerald III
(To:TA_10206400)

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.

 

Skjermbilde 2023-04-24 kl. 23.44.31.png

Werner_E
25-Diamond I
(To:TA_10206400)

You would have to define what should happen if the two vectors are not of equal length.

 

  1. A has fewer values than B.
    The extra values in B may simply be ignored
    OR
    they may be appended to vector A.

  2. A has more values than B (as in your example).
    You may leave the extra values in A unchanged
    OR
    you compare them with the last value in B and replace them by this value if B is larger.

Here an approach which uses vectorization. But it only work if both vector have the same length

Werner_E_0-1682377592525.png
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

Werner_E_1-1682377705570.png

 

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).

Werner_E_2-1682377803050.png

 

 

 

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..... 😅

Werner_E
25-Diamond I
(To:TA_10206400)

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:

Werner_E_0-1682425165076.png

 

Using the vector approach you seem to favor it looks like

Werner_E_1-1682425214774.png

 

Most versatile is to set up functions

Werner_E_2-1682425240638.png

which then can either be used with single input triples

Werner_E_3-1682425271514.png

or with input vectors (don't forget to vectorize)

Werner_E_4-1682425311006.png

 

 

 

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.

Skjermbilde 2023-04-25 kl. 15.27.22.png

 

 

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)".

Skjermbilde 2023-04-25 kl. 15.46.05.png

Sorry for not attaching work sheet.

 

Werner_E
25-Diamond I
(To:TA_10206400)

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.

Announcements

Top Tags