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

Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X

Programming nested array with variable size

ochristensen
2-Explorer

Programming nested array with variable size

Hi

I'm calculating (programming) with nested arrays (MC15). I do two for loops, where the first is fixed in size, but the one inside this is dependent on the size of the nested parts in the array I use for the calculation. My problem is, that when I do this dependent sizing, I seems to work fine as long as the nested parts get larger, but if they are smaller than the largest nested part that have been calculated previously, the loop keeps this size, and fills in the "cells" which doesn't match the smaller part with the value from the larger part.

I hope this not a too cryptic explanation of my problem. I've attached and example of what I'm trying to explain.

ACCEPTED SOLUTION

Accepted Solutions

5 REPLIES 5

Hello Ole,

I could replicate this weird behaviour although I neither have an explanation nor a solution.

Raiko

Add one line!

Thanks for the fix. Can you explain why this reset of the array is necessary?

wicked!

The effect is as it should be.

In your program you use a local variable "sub" and as usual in Mathcad a variable is created as soon as you assign it a value. vectors grow, when you assign elements.

That means, that v[12:=5 creates a vector with 12 (or 13, depending on ORIGIN) elements - all but the last being zero. But if v alreadfy was a 20 element vector it remains a 20 element vector and only the element with index 12 is changed to 5.

This is exactly what happens in your program. Whenever sub has grown to a 3 x 1 vector it never shrinks (why should it?) unless you tell it so.

The simplest way is to set sub <-- 0  or sub <-- NaN  as the first line in the calculation of R (where Fred had used sub <-- (0) ).

Here are two other ways to achieve the same effect:

Whenever a variable name is used LHS of a definition, a local copy is created. So the worksheet M is not tampered, as you can see.

Announcements

Top Tags