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

Stacking a Matrix with a For Loop

  • January 18, 2024
  • 1 reply
  • 3610 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

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