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

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

Redefining function result as a vector

JP_9979155
3-Visitor

Redefining function result as a vector

Hi all,

I am facing the following problem:
I am defining a function with four variables, three of them jsut a number, and one is defined as a series or sequence. (sorry if incorrect)
I would like to obtain the maximum of the resulting sequence after the function evaluates the variables, but it looks that this is not exactly a matrix, since the max function is not working correctly:

JP_9979155_0-1683028870945.png

thanks all!

 

PD: I have seen some solutions but I am unable to open those files. 
@MarkArmstrong 

https://community.ptc.com/t5/Mathcad/Create-a-vector-from-the-result-of-a-function/m-p/191688/highlight/true#M75105

 

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions


@JP_9979155 wrote:

Hi all,

I am facing the following problem:
I am defining a function with four variables, three of them jsut a number, and one is defined as a series or sequence. (sorry if incorrect)
I would like to obtain the maximum of the resulting sequence after the function evaluates the variables, but it looks that this is not exactly a matrix, since the max function is not working correctly:

JP_9979155_0-1683028870945.png

thanks all!

 

PD: I have seen some solutions but I am unable to open those files. 
@MarkArmstrong 

https://community.ptc.com/t5/Mathcad/Create-a-vector-from-the-result-of-a-function/m-p/191688/highlight/true#M75105

 

 

 

 

 

 


You are absolutely right. What you create is not a vector/matrix but an undefined structure and therefore cannot be assigned to a variable or otherwise used later in subsequent calculations.

The reason is a misuse of a range (alpha).

The difference between a range and a vector is a constant stumbling block in Mathcad and Prime, and it actually got a little worse in Prime because the two always look identical.
You are not alone in having problems with this distinction and it is understandable that you fall into this trap.

 

Ranges are only meant for three use cases:
1) For indexing vectors and matrices.
2) As abscissa values in a 2D plot.
3) Within a program in a For loop

 

Your problem can be solved by
1) using a vector instead of a rank for alpha
2) vectorizing the call to the function N. This may not always be mandatory, as Prime often also automatically applies implicit vectorizing (depends on the calculations used in the function N), but should therefore always be used to be safe when passing a vector as an argument to a function, but wanting its elements to be inserted into the function one at a time.

 

There are different ways to create the vector alpha you need. A very convenient and comfortable one is to use an undocumented trick. If you follow the definition of a range immediately by an inline evaluation, the range is turned into a vector.

You won't see any differences when you display it, but you see the difference when you try to access a single "element".

In the following screen shot it fails for beta because its a range and you may think of a range as being an implicit loop rather than e sequence of numbers:

Werner_E_0-1683034697592.png

 

Here is a more "legal" way to create the vector alpha

Werner_E_1-1683034995472.png

 

What Mike (not Mark 😉 ) had posted in the thread you linked to was a utility function which could be used to create a vector, given the first and last element and the number of equally spaced values it should contain. What he posted was just a jpg picture, so you sure should be able to open it.

When you search the forum you may find quite a lot of different utility functions to create a vector.

Here is another one which follows the range definition - its arguments are first, second and desired last element of the vector  (which might not be taken as a value, depending on the first and second value - similar to the behaviour of a range definition):

Werner_E_2-1683036352525.png

Werner_E_4-1683036698429.png

 

For future questions don't forget to attach your worksheet and also state, which version of Prime you are using.

 

View solution in original post

1 REPLY 1


@JP_9979155 wrote:

Hi all,

I am facing the following problem:
I am defining a function with four variables, three of them jsut a number, and one is defined as a series or sequence. (sorry if incorrect)
I would like to obtain the maximum of the resulting sequence after the function evaluates the variables, but it looks that this is not exactly a matrix, since the max function is not working correctly:

JP_9979155_0-1683028870945.png

thanks all!

 

PD: I have seen some solutions but I am unable to open those files. 
@MarkArmstrong 

https://community.ptc.com/t5/Mathcad/Create-a-vector-from-the-result-of-a-function/m-p/191688/highlight/true#M75105

 

 

 

 

 

 


You are absolutely right. What you create is not a vector/matrix but an undefined structure and therefore cannot be assigned to a variable or otherwise used later in subsequent calculations.

The reason is a misuse of a range (alpha).

The difference between a range and a vector is a constant stumbling block in Mathcad and Prime, and it actually got a little worse in Prime because the two always look identical.
You are not alone in having problems with this distinction and it is understandable that you fall into this trap.

 

Ranges are only meant for three use cases:
1) For indexing vectors and matrices.
2) As abscissa values in a 2D plot.
3) Within a program in a For loop

 

Your problem can be solved by
1) using a vector instead of a rank for alpha
2) vectorizing the call to the function N. This may not always be mandatory, as Prime often also automatically applies implicit vectorizing (depends on the calculations used in the function N), but should therefore always be used to be safe when passing a vector as an argument to a function, but wanting its elements to be inserted into the function one at a time.

 

There are different ways to create the vector alpha you need. A very convenient and comfortable one is to use an undocumented trick. If you follow the definition of a range immediately by an inline evaluation, the range is turned into a vector.

You won't see any differences when you display it, but you see the difference when you try to access a single "element".

In the following screen shot it fails for beta because its a range and you may think of a range as being an implicit loop rather than e sequence of numbers:

Werner_E_0-1683034697592.png

 

Here is a more "legal" way to create the vector alpha

Werner_E_1-1683034995472.png

 

What Mike (not Mark 😉 ) had posted in the thread you linked to was a utility function which could be used to create a vector, given the first and last element and the number of equally spaced values it should contain. What he posted was just a jpg picture, so you sure should be able to open it.

When you search the forum you may find quite a lot of different utility functions to create a vector.

Here is another one which follows the range definition - its arguments are first, second and desired last element of the vector  (which might not be taken as a value, depending on the first and second value - similar to the behaviour of a range definition):

Werner_E_2-1683036352525.png

Werner_E_4-1683036698429.png

 

For future questions don't forget to attach your worksheet and also state, which version of Prime you are using.

 

Top Tags