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

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

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

anthonyQueen
11-Garnet

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

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.

ACCEPTED SOLUTION

Accepted Solutions

ravel

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

View solution in original post

27 REPLIES 27

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

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

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

nas0k
12-Amethyst
(To:ELSID)

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

Norm

Thanks Norm for the suggestion!

Sorry for the naive question: where can I find it? it does not seem a function of mathcad.

Oops.... got it. But it is a function of Mathcad Prime 2, which I avoid for the while.

Parentheses:

And the prime reason I avoid it is not only because it still lacks some functionality present in Mathcad, but for the fact that it is a "huge" installation, and a FrameNet Application, which I do not like. This is the reason why I still use v.11: and it is really great, with a space that takes 50Mb of HD (not Gbs) and few Mb of RAM. I call it efficient.

ravel

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

Fred Thanks and Well done!!

I am studying how you did. Unfortunately after translating it to v.11 the solution gives some errors.

If you may clarify, how did you write:

rowS(A):=0 on error rows(A)

with spaces, while still managing to keep it as a single math definition (so it seems). And what is its purpose?

Many thanks again!

Anthony

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

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

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.

Don't worry about it. If I choose to answer someone's question I do so because I can, not because I am looking for points. From my viewpoint, I get my payback when I go on a forum for something where I'm not an expert, and someone helps me. What goes around, comes around

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

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

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.

Richard Jackson:

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.

Thanks Richard for your clarification and help. Unfortunately I cannot see the image.

If I understand correctly what it does p(a)[0,0, is not that "it evaluates the expression [(b*A)*a] once for each value". In that case we would have the whole standard result p(a). But it evaluates only the first element of b*A (i.e. element 0,0). So the pedix refers to the result (b*A) before it starts to iterate.

The image is just the one you posted. For some reason it didn't show in the reply.

But it evaluates only the first element of b*A (i.e. element 0,0). So the pedix refers to the result (b*A) before it starts to iterate.

Exactly. It evaluates the first element only, three times. Only one value from the range is passed to the expression each time it is evaluated.

Thanks Richard also for the file above! You have done a wonderful job in explaining and resolving the issue.

Many thanks indeed.

Yes, I was working with MC15 and I was afraid already that there could be some version problems - thats the reason for the screenshots.

I guess that the example in your last post would work if you assign p(a)_0,0 and p(a)_0,1 to variables and let augment work on them. But of course this would be useless for your purpose as you could not create a function r(a) that way. No clue why the assignment to a variable solves those problems. Furthermore there seems to be a great difference between a normal assignment and a local assignment inside a program in this respect.

Good luck and let us know if you come up with something new.

WE

RichardJ
19-Tanzanite
(To:Werner_E)

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

See my other post.

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

That and setting the x-axis range for plots. Other than those two things, unless there is a very good reason to do otherwise, use vectors for everything.

Werner_E
25-Diamond I
(To:RichardJ)

It does not explain why the same function evaluates differently if its output(!) is assigned to a variable or not.

It looks like that that "display of results" you were talking about, is converted to a real vector by an assignment!?

RichardJ
19-Tanzanite
(To:Werner_E)

I don't think there is a good explanation for that

It looks like that that "display of results" you were talking about, is converted to a real vector by an assignment!?

With a prgram, that seems to be the case. But when there is just an assignment with no program, for example as shown in the worksheet I posted, it does not work that way.

Mathcad has it's quirks, and this is one of them.

Werner_E
25-Diamond I
(To:RichardJ)

I have just looked at your worksheet and your function to convert a range variable to a vector works correctly only because af the if condition at the end. I dont't think that this was build that way for that reason.

Even the ravel function of Tom Gutman suffers from the problem that it does not do what it should if you do not assign it to a variable but use its output as a function argument. Nevertheless I appreciate his way to increment the index. I think using your converter in combination with the function p3 should be a solution to Anthony's question if he gets it to work with Verson 11.

Below a modification to your convertert as I wanted to see what is happening. The dimension of the local vector v changes according to the result being assigned to a variable or not. I see it but I still don't understand whats going on. But as you said - Mathcad has its quirks.

16.11_4.png

RichardJ
19-Tanzanite
(To:Werner_E)

I have just looked at your worksheet and your function to convert a range variable to a vector works correctly only because af the if condition at the end. I dont't think that this was build that way for that reason.

I wrote that function, so actually it probably was built that way because it made it work. I don't recall my reasoning for doing that way though. It's possible there was no logic behind it; I just played around until it worked as well as possible. There was an old discussion a long time ago (back in the Mathsoft days) where we compared various versions of this function, including ravel. Some worked only when passed a range, but not when passed a range variable, some worked with the symbolic processor, some not, some gave the correct output when either evaluated or assigned to a variable, some not. The reason I use the variation I do is because it worked in more cases than any of the others. I've attached the comparison worksheet.

Werner_E
25-Diamond I
(To:RichardJ)

Thanks for your information.

I just tried to include your function in my second try to solve Anthonys problem as it seemed that it returns a true vector in every case but I had to find out that calling the function without an assignment still return something which only looks like a vector. Bad for Anthony.

16.11_5.png

Next try, but still the same effekt:

16.11_2.png

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.

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

Announcements

Top Tags