Skip to main content
1-Visitor
January 18, 2024
Solved

Stacking a Matrix with a For Loop

  • January 18, 2024
  • 1 reply
  • 3656 views

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?

Best answer by Werner_E

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

 

 

1 reply

Werner_E25-Diamond IAnswer
25-Diamond I
January 18, 2024

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

 

 

1-Visitor
January 18, 2024

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.

25-Diamond I
January 18, 2024

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