Skip to main content
8-Gravel
March 5, 2025
Solved

Invalid Array Index (variable in index)

  • March 5, 2025
  • 1 reply
  • 3530 views

Hello,

 

I am using Mathcad Prime 10.0.0.0.  On sheet 3 I am trying to call in column 2 (via data[i,2] in the first IF statement) which has a text name for the value.  If the value equates to something in my IF statements, I want my program to calculate the H_f or Friction Head.  I want the program to go down the array one at a time and at the end output the sum of all the individual friction heads added together, so I gave the array the indices of i,2 with i going from 0 (first value in the submatrix 'data') to p-1 (end of the rows in the main Matrix 'System').  I am not sure why I am receiving an error which states "This array index is invalid.  The index must be an integer, not less than origin, and not greater than the last element.".  Is there any chance you all may be able to assist?  Can a variable not be in the Array index?  If not, how do I go down my list?  Should I have individual vectors (single column arrays) for each column in the matrix and call them out that way?  Seems more time intensive if I go that route.

 

V/r,

DM

Best answer by Werner_E

Your for loop runs from 0 to end=5, but the last row index of your matrix "data" is just 4. So when i=5 the error is thrown.

 

EDIT: Had not looked at your new sheet.

In your new sheet variable "end",  which you still use as end value of your for-loop, is not defined at all - hence the error.

 

You increment variable "a" in the for loop in each cycle, so "i" and "a" end up having the same value in each loop cycle. So "i+a" is equivalent to "2*i".

No idea what you are about to achieve, but the row index you use must never exceed the last allowable row index (which is 4 in case of your matrix "data").

 

EDIT 2: Your pdf shows a different file as the one you posted!! Here you have to change p-1 to p-2. After all p is the number of rows of the matrix "System". The matrix "data" has one row less, so the last allowable index is p-2.

The program in your pdf still increments variable a without using it. You may delete that expression.

The same goes for the statement i <-- i+1 at the end. It has no effect an is not used in any way.

1 reply

25-Diamond I
March 5, 2025

You are incrementing variable "a" in your loop, so eventually the expression "i+a" which you use as row index for matrix "data" exceeds the value 4 (index of the last row in "data") and so you are trying to access a non existent row - this is what the error message tries you to tell.

 

Are you here with two different accounts? Its noticeable that the sheet posted here
Help with program

contains the same structure and the same errors (starting with the calculation of β€˜end’) as the one posted here.

8-Gravel
March 5, 2025

Werner, thank you for the response.  I had asked someone at PTC for assistance and it appears they handed off my question/program to  JH_9781973.  I read that thread, but it wasn't focused on my main concern.

 

I removed the [i+a, j+b] and made my index [i, #]  .  I also changed the end of the for loop from p to p-1.  Do you know why I get an error for having i as my row when I am incrementing i?

Werner_E25-Diamond IAnswer
25-Diamond I
March 5, 2025

Your for loop runs from 0 to end=5, but the last row index of your matrix "data" is just 4. So when i=5 the error is thrown.

 

EDIT: Had not looked at your new sheet.

In your new sheet variable "end",  which you still use as end value of your for-loop, is not defined at all - hence the error.

 

You increment variable "a" in the for loop in each cycle, so "i" and "a" end up having the same value in each loop cycle. So "i+a" is equivalent to "2*i".

No idea what you are about to achieve, but the row index you use must never exceed the last allowable row index (which is 4 in case of your matrix "data").

 

EDIT 2: Your pdf shows a different file as the one you posted!! Here you have to change p-1 to p-2. After all p is the number of rows of the matrix "System". The matrix "data" has one row less, so the last allowable index is p-2.

The program in your pdf still increments variable a without using it. You may delete that expression.

The same goes for the statement i <-- i+1 at the end. It has no effect an is not used in any way.