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

Min function

YA_10963798
14-Alexandrite

Min function

Just one question about ( min function) why it works only with scalars ... whenever I want to use it with vectors or matrics  it doesn't work ... 

How to overcome this ? 

Should I always use programming ?

 

For example the following program : in each (qc;gem;cor ) I have 3 matrics each one has ten values.... what I need is to write a program that will compare the both variables and choose the minimum value of each one ( compare the first raw with the first raw  and chooses the min ...and so on  ) ...and the result should be also one matric with ten values. However, it chooses only the minimum value of each matrics 

YA_10963798_0-1729002032652.png

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:StuartBruff)

It seems to me that @YA_10963798  would need you function but for only two inputs.

Applied to the simpler example posted in the other thread this would mean

Werner_E_0-1729009893915.png

BTW, I seem not to grasp the necessity of your local function minv?

Prime 9 sheet attached.

 

View solution in original post

15 REPLIES 15
StuartBruff
23-Emerald III
(To:YA_10963798)

Try something like the function Min shown below.  It recursively calculates the minimum of each nested array ... well, it's supposed to!

 

2024 10 15 C.png

 

The last one is messy but interesting.

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

Hi Stuart, 

I tried it 

YA_10963798_1-1729006447954.png

 

Actually what I need is something different I need the program to compare qc;gem;1 and qc;gem;2 and choose the minimum between both of them.

So , it goes to the first raw in both variables and choose the min and then goes to the second one and does the same. 

but what we did is just choose the min value in each matric

 

Werner_E
25-Diamond I
(To:YA_10963798)

Why not post a simple example sheet so we have something to play with?
See also my suggestions in the other thread...

StuartBruff
23-Emerald III
(To:YA_10963798)

Ah, my apologies.  I was distracted by another problem and didn't read your message correctly.  I just caught sight of the word "minimum", saw a nested vector, and ran off like a dumb dog chasing a rabbit instead of a hare (not that a modern, socially aware. dog ought to be chasing any fellow creatures, of course).

 

Is this any better?

 

2024 10 15 D.png

 

Stuart

YA_10963798
14-Alexandrite
(To:StuartBruff)

I will try it right away .. Thank you

Werner_E
25-Diamond I
(To:StuartBruff)

It seems to me that @YA_10963798  would need you function but for only two inputs.

Applied to the simpler example posted in the other thread this would mean

Werner_E_0-1729009893915.png

BTW, I seem not to grasp the necessity of your local function minv?

Prime 9 sheet attached.

 

StuartBruff
23-Emerald III
(To:Werner_E)


@Werner_E wrote:

It seems to me that @YA_10963798  would need you function but for only two inputs.

Applied to the simpler example posted in the other thread this would mean

Werner_E_0-1729009893915.png

BTW, I seem not to grasp the necessity of your local function minv?

 

Probably due to sheer laziness on my part.  You've externally defined and vectorized minv (as minvecs2) whereas I put it inside minvecs; AFAICT, it's needed either way.  The lazy way means minvecs is self-contained and I don't need to remember to vectorize minvec2 or, worse, potentially vectorize it more than once!! 😱 I have a self-imposed per diem keystroke limit - my personal contribution to saving energy and reducing anthropogenic global warming.  😇

 

Then again, I might very well have missed something that you've spotted and am still failing to see it.  ☹️

 

Stuart

 

I've miscopied vectorized expressions more than once because I didn't fully enclose the expression, so I tend to avoid explicit vectorization, preferring to hide it away in a function where possible.

Werner_E
25-Diamond I
(To:StuartBruff)

Ahh, I missed the fact that in your version its not necessary to vectorize the function call when used with vector arguments.

Usually I prefer to explicitly vectorize when I call a function with vector elements and want the operation to be done element-wise - not least to remind myself of what is happening at that point. But now I feel guilty of being more responsible for global warming than is absolutely necessary 😞
And besides global warming effects, a function which is written to take vectors as its input and apply the vectorization itself sure has the benefit that the function calls look less ugly and confusing as of the arrow above them.

StuartBruff
23-Emerald III
(To:Werner_E)


@Werner_E wrote:

Ahh, I missed the fact that in your version its not necessary to vectorize the function call when used with vector arguments.

Usually I prefer to explicitly vectorize when I call a function with vector elements and want the operation to be done element-wise - not least to remind myself of what is happening at that point


 

Horses for courses.  Mathcad, like many other such applications, vectorizes many of its built-in functions, and so it makes sense to me to do the same.  In addition, the fact that function arguments are effectively global variables allows the programmer to remove some of the burden of trying to vectorize operations over (knowingly) disparate array dimensions.  I frequently use this ability to handle pseudo-vectorization of functions of the form f(n, v) where not only does f vectorize scalar n over vector v, but it can also vectorize over vector n as well.  Most users do not want to know about such degrees of complexity and simply want "it" to happen.   

 

But, this approach can catch people out if they're not expecting it, and it makes the functions a lot more complex in themselves and, therefore, harder to verify and understand.  Not to mention usually taking up more valuable whitespace than a simpler function would!  😱

 


@Werner_E wrote:

But now I feel guilty of being more responsible for global warming than is absolutely necessary 😞


And so you should, Werner, so you should.  👿

 


@Werner_E wrote:

And besides global warming effects, a function which is written to take vectors as its input and apply the vectorization itself sure has the benefit that the function calls look less ugly and confusing as of the arrow above them.


Indeed.  Especially if you're playing with complex numbers and want to indulge in wanton use of the complex conjugate ... on a 4K screen with Mathcad at 80% zoom.  😵

 

Of course, if we could use postfix operators, I might use the * form of complex conjugate operator instead (as per ISO 80000-2:2019).

 

Stuart

StuartBruff
23-Emerald III
(To:StuartBruff)


@StuartBruff wrote:

@Werner_E wrote:

Ahh, I missed the fact that in your version its not necessary to vectorize the function call when used with vector arguments.

Usually I prefer to explicitly vectorize when I call a function with vector elements and want the operation to be done element-wise - not least to remind myself of what is happening at that point


 

Horses for courses.  Mathcad, like many other such applications, vectorizes many of its built-in functions, and so it makes sense to me to do the same.  In addition, the fact that function arguments are effectively global variables allows the programmer to remove some of the burden of trying to vectorize operations over (knowingly) disparate array dimensions.  I frequently use this ability to handle pseudo-vectorization of functions of the form f(n, v) where not only does f vectorize scalar n over vector v, but it can also vectorize over vector n as well.  Most users do not want to know about such degrees of complexity and simply want "it" to happen.   

 

But, this approach can catch people out if they're not expecting it, and it makes the functions a lot more complex in themselves and, therefore, harder to verify and understand.  Not to mention usually taking up more valuable whitespace than a simpler function would!  😱


Here's an example of making things more complex but with added generality of function.   Which output would people prefer, if either?  And which would they like to see as a solution to their specific problem?  Note: structure-preserving means that the overall shape of the nested array is preserved, ie the fact that an element was an array or not an array. It does not preserve the size of the nested elements, nor should it, as min is a reducing function.

 

2024 10 15 F.png

 

 

2024 10 15 E.png

 

The first variant of Min could be readily made into a structure-preserving function by copying over the last line in the function body of the second variant.

 

Stuart

Werner_E
25-Diamond I
(To:StuartBruff)

I like it, but I am not sure if many people would expect a 1x1 matrix as the result of a vector or matrix argument.

But of course that is what structure preserving means.

Nonetheless I'm sure that some people would prefer

Werner_E_0-1729024387654.png

But then, its all a matter of what you need the function/result for in the further calculation.

Its similar to match or lookup, where you often just need the index number or the matching element and not a 1x1^matrix, which is returned by the built-in functions. They do this for good reason, as there could be more than one match and it would complicate a lot of applications if the functions would return a scalar instead if a 1x1 matrix.

 

StuartBruff
23-Emerald III
(To:Werner_E)


@Werner_E wrote:

I like it, but I am not sure if many people would expect a 1x1 matrix as the result of a vector or matrix argument.

But of course that is what structure preserving means.

Nonetheless I'm sure that some people would prefer

Werner_E_0-1729024387654.png

But then, its all a matter of what you need the function/result for in the further calculation.

Its similar to match or lookup, where you often just need the index number or the matching element and not a 1x1^matrix, which is returned by the built-in functions. They do this for good reason, as there could be more than one match and it would complicate a lot of applications if the functions would return a scalar instead if a 1x1 matrix.

 


Indeed.  When I could reasonably expect to see a vector result, I normally ensure that valid results return a vector, even if is a 1x1.  I frequently use 0 (zero) to indicate that there was no reasonable vector result, ie 0 acts a stand in for the empty array (*).  An example is when I want to filter a list using, say, match but there are no elements in the list that meet the match criterion.  In that case, 0 usually makes far more sense than raising an error (as built-in match does) - it tells me exactly what I need to know: there were zero matches.   As even a single match returns a vector, it's easy to check whether there are any matches. And iI can do so without having to build in error traps and consume unnecessary vertical whitespace using the try-else operator. 

 

Stuart

 

(*) It doesn't cover all cases, which is why we still need an empty array.  Ideally accompanied by a true multi-dimensional array (MDA) data type! 😁

Werner_E
25-Diamond I
(To:StuartBruff)


(*) It doesn't cover all cases, which is why we still need an empty array.  Ideally accompanied by a true multi-dimensional array (MDA) data type! 😁


Sure! Dreaming is not forbidden, one can still keep dreaming ...

StuartBruff
23-Emerald III
(To:Werner_E)


@Werner_E wrote:

(*) It doesn't cover all cases, which is why we still need an empty array.  Ideally accompanied by a true multi-dimensional array (MDA) data type! 😁


Sure! Dreaming is not forbidden, one can still keep dreaming ...


 

Anything not permitted is forbidden.

 

Plus, have you seen how much energy the brain uses when dreaming?

 

Sleep interrupts the connection with the external world, but not the high cerebral metabolic demand. First, brain energy expenditure in non rapid eye movement (NREM) sleep only decreases to ~85% of the waking value, which is higher than the minimal amount of energy required to sustain consciousness []. Second, rapid eye movement (REM) sleep is as expensive as wakefulness and the suspension of thermoregulation during REM sleep is paradoxically associated with increases in brain metabolic heat production and temperature 

https://www.ncbi.nlm.nih.gov/pmc/articles/PMC5732842/

 

It won't be long before somebody demands we give up dreaming to slow down the environmental disaster that 2 billion dreaming people will cause by their excessive heating of their respective local environments.

 

Stuart

 

Gotta cut back on the typing and dreaming ...

 

Oh, what the heck.  Venus Mk II here we come!

Werner_E
25-Diamond I
(To:YA_10963798)

It sure may be a good idea to open a new thread for a different question. Just try to avoid to ask the same question twice.

Stuarts approach here sure is way more generic than my basic attempt there -> Solved: Re: Minimum values depending on layer thickness

I am not sure how to use his function to get the same result (for v1 and v2 in my example there)

But then its still unclear to me which single 10x1 vector you would expect as result in your example. See my remarks in the other thread for this.

Announcements

Top Tags