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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Stacking a Matrix with a For Loop

JS_10925694
2-Guest

Stacking a Matrix with a For Loop

I'm struggling to get a for loop to run in Mathcad Prime 7.0.0

 

I have a matrix "d" that I want to stack "N" times. The matrix "d" could have any number of rows, and "N" >= 1  (these are defined elsewhere in my sheet). But I cannot for the life of me figure out how to iterate through the loop. Where have I gone wrong?

 

The examples I see are using "i" inside the loop to perform an operation (sum several terms, etc.). Maybe since I'm not actually using "i" for the calculation anywhere it's breaking? Maybe because my stacked matrix isn't actually a function defined by "i"? Should I be using while instead of if?

1 ACCEPTED SOLUTION

Accepted Solutions

Its always better to attach the worksheet itself, not just a picture!

 

You do not want to define a function, so simply assign the program to a normal variable D

Do not change the loop variable i inside the loop (unless you know exactly what you do and what PTC did when they implemented programmed loops).

The loop must run from 1 to N-1 or from 2 to N because you already initialized the local variable D with a row of values.

 

Try this:

Werner_E_0-1705602579915.png

 

Here is an alternative using the row operator

Werner_E_1-1705602803421.png

Did not know which value you have ORIGIN set to, so I made the program ORIGIN-aware.

If ORIGIN=0, then i  in  0 .. N-1

If ORIGIN=1, then  i  in  1 .. N

 

 

View solution in original post

6 REPLIES 6

Its always better to attach the worksheet itself, not just a picture!

 

You do not want to define a function, so simply assign the program to a normal variable D

Do not change the loop variable i inside the loop (unless you know exactly what you do and what PTC did when they implemented programmed loops).

The loop must run from 1 to N-1 or from 2 to N because you already initialized the local variable D with a row of values.

 

Try this:

Werner_E_0-1705602579915.png

 

Here is an alternative using the row operator

Werner_E_1-1705602803421.png

Did not know which value you have ORIGIN set to, so I made the program ORIGIN-aware.

If ORIGIN=0, then i  in  0 .. N-1

If ORIGIN=1, then  i  in  1 .. N

 

 

The rest of my document is a work in progress and I was too ashamed to share it. I've got other error warnings all over, it's poorly arranged, incomplete equations, miscellaneous notes, etc. Trying to combine a few of my company's excel sheets and some other engineer's incomplete attempts into one document for a smoother experience.

 

The row operator method works perfectly! Problem solved!

The adjusted for loop does not, same result as before where it only displays the single line results. It doesn't need to be done that way, it's just the easiest way I thought I could stack up or augment my matrices.

 

Thank you Werner, you're a gift to humanity.

You could have prepared a small sample worksheet which just shows your problem (usually thats even better than attaching a 30 page document with "the error is in page 23" 🙂 )

 

The modifications I suggested for your original routine should work equally well. See here:

Werner_E_0-1705606497793.png

 

Looked further into it and found that your original routine should work OK as well (even though neither the function argument i nor the line where you change the value of i make much sense). You can see that you get one row too much - the reason I suggested N-1 as last value for i.

Werner_E_2-1705606905702.png

I was surprised to see that Prime allowed the inline evaluation of this function definition. I had expected it to throw an error - you never stop learning 😉  Seems that Prime realizes that the argument i has no effect because its overwritten by the loop range 1..N and therefore the result is independent from the argument and can be displayed.

 

I looked further at your pic and found the culprit. It seems to be one of Primes infamous auto-labelling errors.

If you look closely at the pic you can see that the encircled "D" is printed differently than the rest - its upright and not italic. This means that its not labelled as variable but as something else, maybe as "function".

Werner_E_3-1705607553312.png

So Prime treats it as if it were a completely different variable and so the local variable D which was initialized with d^T never is changed and thats the reason you get just this one row as a result.

When you type "D" Prime tries to label it correctly as "Variable", "Function", "Unit", etc. whatever it thinks is appropriate. But especially when you start to edit a region more heavily it fails now and then and this seemed to have happened here. Its also possible that Prime wasn't doing anything wrong but you inadvertently pressed the key combination to change the label. Can't be said anymore now.

 

But this error is a good example which demonstrates that attaching the worksheet is much better than just attaching a picture because with the sheet at hand this error would have been far easier to detect. 😉

 

My apologies again for not sharing it the first time, thought it was a trivial issue to solve (and clearly it was since you had two simple solutions). Will do better next time.

 

Every example I found of how to define a for loop included f(i) so I just presumed that it was a necessary step. I agree it doesn't make much sense, but as I'm brand new to this I thought "Without defining it as a function of i, how could it be looping for i terms?" And then when it broke I suspected that this part which was not making much sense was the culprit.

The tricky part for me was when I tried to replicate the error after your second reply: it worked just fine. I was so perplexed. I retyped it several times in various sheets (old and new), and every time it just worked. I had long deleted my original testing zone where I had written the bit I sent in the attachment, lost the original so I was just stuck with the knowledge and shame that if I just removed the (i) definitions it would have worked out for me all on my own. Had it produced one too many rows I could have easily figured out that subtracting one from N would get me where I wanted.

 

Perhaps the non-italicized D came from the text formatting of a previously (mostly) deleted "for" or "stack" function as I was working my way through the learning.

Yes, its very likely that editing the program somehow changed the label of that variable and if one is not really aware of variable labels this sure will go unnoticed (and even if you are aware of it you ever so often fall in that trap).

If simply typing "for" was disappointing because this did not create a loop (its simple text), you might want to know of a nice feature of Prime. If you type "for" (without the quotes) immediately followed by typing Ctrl-J, the correct programming structure for the loop is created. The same works with all other programming commands like "if", "while", etc.

 

As for the errors you made - that's the way we all are approaching new and unknown territory. There is nothing wrong with learning by trial and error.

 

And yes, programs usually are used in conjunction to (utility) programs but can also be used to simply calculate the value for a single variable.

If your application requires you to use a similar stacking procedure multiple times, it would also make sense in this case to write an auxiliary function for this (you can hide it at the beginning of the sheet in a collapsed area or in the non-printable area to the right of the main page) and then only use this. This function would not have "i" as its argument (as "i" is defined locally anyway) but rather the row vector you would like to be multiplied and the number of times you'd like to see it.

For example

Werner_E_0-1705618933675.png

 

And because you asked for a while-loop in your first posting, here is a version using it

Werner_E_2-1705620187307.png

 

But for being most versatile and also able to stack column vectors or matrices, I would prefer an approach similar to the one you had posted:

Werner_E_3-1705620253149.png

All three work OK for row vectors or scalars, but only the last one will also deal correctly with matrices of more than one row. See her for a comparison:

Werner_E_5-1705620473289.png

 

 

 

 

 

 

Top Tags