Skip to main content
11-Garnet
November 16, 2012
Solved

how to turn nested arrays (or tables) into a "clean" matrix

  • November 16, 2012
  • 6 replies
  • 13065 views

I kindly ask what is the simple way to turn the following result in a matrix form (Matchad v.11 used):

a: is a range variable, with 3 values.

A: is a matrix 2x2 or similar size

b: a row vector 1x2, or compatible size

When I try: p(a)=b*A*a

In my case the results of p(a) are shown in matrix form with expansion of the arrays (from the Menu Format/Results).

Question: how do I turn the result p(x) in a "clean" matrix of size 3x2 (each row showing the result for a given value of "a"), with no nested results?

I tried the function "stack" but it did not work. Thanks for any feedback.

Best answer by Fred_Kohlhepp

ravel

courtesy of Tom Gutman (pause for brief moment of respectful silence.)

6 replies

12-Amethyst
November 16, 2012

Hi Anthony,

You don't need to use the subscript operator on the output of the program.

if you call the function with 'Temperature[p' then mathcad will calculate for each element of the temperatuer array , which Ithink is where the extra looping originates.

See attached.

regards

Andy

23-Emerald I
November 16, 2012

Somewhere in this site is a sheet with a program to turn a range variable into a vector. Then your equation becomes simple:

1-Visitor
November 16, 2012

range2vec. IT was published in Essential MathCAD 2nd edition by Brent Maxfield. This can be found in the "scribd" webpages looking up the book ... in case you can not find the sheet

http://www.scribd.com/doc/62950142/Essential-Mathcad-for-Engineering-Science-And-Math-2nd-Ed

1-Visitor
November 16, 2012

Also, look up "ravel", a program that takes a range varable and turns it into a single column vector

Norm

11-Garnet
November 16, 2012

@ A Westerman, Fred Kohlhepp, ELSID

Many thanks for your feedback, which are all helpful.

  • Fred solution gets the result, but it avoids range variables from the start. Good workaround, though I am still looking for if one can turn p(a) above to a matrix.
  • A. Westerman file does not really deal with both range variables and matrixes. In my case what complicates the issue a bit is that the range variable is not sequential+1, but is a fraction of a maximum value (which may change itself and it is not a natural number).
  • ELSID reference was helpful: I have the 1st ed. book (the range2vec is presented in advanced programming). Yet it does not resolves the question. It just turns a neat range variable to a matrix, but it does not turn to a matrix the outcome p(a) above that is a range variable with nested matrixes.
19-Tanzanite
November 16, 2012

Hopefully this will help.

The key thing that you are missing is that ranges are not arrays of values. They are iterators, like a loop variable. A calculation containing a range is evaluated once for each value in the range. The results may be displayed in the form of a vector, but it is not a vector. It is just a list of results, one for each value in the range. That's why you can't assign the results list to a variable. The only way to achieve what you want is to turn a into a vector.

A really good rule of thumb is "don't use range variables for anything other than indexing arrays and setting the x-axis range for plotting".

11-Garnet
November 16, 2012

Richard I am sorry to not give you the deserved "Right answer" Your file is even more complete than that of Fred, but his answer came just a dozen of minutes before.

I am really greateful for your explanation. the file is really well done and I was able to translate to v. 11 too. Many thanks indeed.

25-Diamond I
November 16, 2012

Have played around with your problem and while it looks like I have found a solution, I found myself clueless concerning a specific side effect.

Maybe someon else can solve that puzzle - the behaviour of Mathcad in this respect sute is not consistent.

Seems the lesson is to keep away from range variables other than for indexing.

WE

16.11.png

11-Garnet
November 16, 2012

Thanks indeed Werner!!

for your time and your effort. The irony of life: after having prised Mathcad v.11, now your file does not work with it (the later p2(a) gives error: I shall investigate why). But it works with v.15.

The first error that you have, may be explained by fact that the result p(a) above is a set of functions: one for each iteration. So probably Mathcad does not accept to assign many functions to a function, yy in your case.

The real problem is that one cannot (without programming) extract single elements from even simple range variables. It is true however that if you write p(a) with pedixes you get part of the nested results. Yet, all the attempts that follow does not get anywhere. See image below, which returns the nested matrix p(a).

Thanks again to get it with programming. Let me see what does not work in v.11

mathcad_range to matrix problem.png

19-Tanzanite
November 16, 2012

The real problem is that one cannot (without programming) extract single elements from even simple range variables.

Because a range variable does not have such elements. It is not an array. It has a start, a second value (which defines the increment) and a stop. That's all. It is used to iterate calculations, and that is all it can be used for.

http://communities.ptc.com/servlet/JiveServlet/showImage/2-192084-41425/mathcad_range+to+matrix+problem.png

When you evaluate p(a)[0,0, it evaluates the expression once for each value of a. So first it gets the value 18.9. Then 34.2, and finally 49.5. It displays the three results in te form of a column vector. It is not a column vector though, which would be one result, it is a display of three results. When you evaluate the expression using augment, first it evaluates it using the first value of a, and the result is a row vector (because you augmented two scalars). Then it evaluates is for the other two values of a, each of which also returns a row vector. It displays the three results in a list.

25-Diamond I
November 16, 2012

Next try, but still the same effekt:

16.11_2.png

11-Garnet
November 16, 2012

Let me say thanks to all contributors. I learnt a great deal and I enjoy your explanations, and I will even more if there will be still some developments. This Mathcad community is an invaluable asset.

On my part I can say (at least partially using some of your very helpful files) what makes the error in Mathcad v.11.

After the programming to turn the range variable "a" into a vector, your definition left unassigned the range variable.

For instance:

Richard: FillVec(X)

Werner: mg2vec(range)

In v.11 it accepts FillVec(2,2.1...2.5), but it gives ERROR if one writes (as both of you did) a:=(2,2.1...2.5), and FillVec(a).

In other words the range should be made explicit within the parenthesis of the programming name-definition. I hope to got it right.

19-Tanzanite
November 17, 2012

Unfortunately, the ability to convert a range to a vector was not really "designed in". Certainly, not in version 11. So you are right that the behavior is a little flaky. As you note, in version 11 you can pass the function a range value, but not a range variable (i.e. a range value assigned to a variable).