Skip to main content
10-Marble
August 8, 2025
Solved

Beginner question: Why does a program "forget" array assignment when returning the result?

  • August 8, 2025
  • 1 reply
  • 429 views

Why does this element by element assignment not work?
MathCAD array Screenshot 2025-08-08 140920.png

Best answer by Werner_E

"length" gives you the number of entries in a vector (BTW "rows" would yield the same result).

That way your loop reads

Werner_E_0-1754657371468.png

and  "loops" just from 3 to 3. The only value "i" is assigned is 3.

Its the same as

Werner_E_0-1754658013667.png

So you just define element with index 3 of your vector. If you do so, by default the other entries with lower indices are set to zero, like

Werner_E_1-1754657492150.png

In this example the value of ORIGIN was not changed and is at its default value 0. So the element with index 3 is the fourth vector entry. In your sheet you obviously had set ORIGIN to 1.

So what you meant was

Werner_E_2-1754657598860.png

The loop start at 1, which is the value of ORIGIN in your sheet, and ends at length(e)=3 (which is only the index of the last vector element if ORIGIN is set to 1)

 

A better approach would be

Werner_E_3-1754657636792.png

because its independent from the setting of ORIGIN.

The "last" function returns the index of the last vector element, taking into account the value of ORIGIN.

 

 

 

 

1 reply

Werner_E25-Diamond IAnswer
25-Diamond I
August 8, 2025

"length" gives you the number of entries in a vector (BTW "rows" would yield the same result).

That way your loop reads

Werner_E_0-1754657371468.png

and  "loops" just from 3 to 3. The only value "i" is assigned is 3.

Its the same as

Werner_E_0-1754658013667.png

So you just define element with index 3 of your vector. If you do so, by default the other entries with lower indices are set to zero, like

Werner_E_1-1754657492150.png

In this example the value of ORIGIN was not changed and is at its default value 0. So the element with index 3 is the fourth vector entry. In your sheet you obviously had set ORIGIN to 1.

So what you meant was

Werner_E_2-1754657598860.png

The loop start at 1, which is the value of ORIGIN in your sheet, and ends at length(e)=3 (which is only the index of the last vector element if ORIGIN is set to 1)

 

A better approach would be

Werner_E_3-1754657636792.png

because its independent from the setting of ORIGIN.

The "last" function returns the index of the last vector element, taking into account the value of ORIGIN.

 

 

 

 

10-Marble
August 8, 2025

Of course. Silly me. I actually have used that construct a couple of times earlier, it just hasn’t become part of “muscle memory” yet.

Thank you for quick reply!

25-Diamond I
August 8, 2025

No problem.

You may consider closing the thread if there are no follow up questions.

For future questions I would suggest that you attach your worksheet (even though its just as small as here) and also state which version of Prime you are using.