Community Tip - Help us improve the PTC Community by taking this short Community Survey! X

Translate the entire conversation x

Array index invalid

fahmed-2
13-Aquamarine

Array index invalid

Hello,

 

I am to calculate lateral stress but am unable to track why my array index is invalid. can you please help to solve my issue

 

fahmed2_0-1744658957089.png

 

Many thanks

 

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:fahmed-2)

The vector "value" you create in the program does not get a value in each turn of the for loop.

I inserted a "try and catch" at the position where the error is thrown

Werner_E_0-1744663526976.png

and it can be seen by the result

Werner_E_1-1744663554858.png

that "value" has just 14 elements when i=25 and so you obviously can't access element #24.

 

You have to check your nested series of "if ... else if .... if ... else ... etc." to assure that "value" is assigned a value in every 'round' of the for-loop.

 

BTW, your "index" function could be written by simply using the "match" function. Only difference is the behaviour if the value (z) is not found in the vector.

Your function returns 0 in this case but "match" will return an error. If this is mandatory you may throw in a try and catch as shown:

Werner_E_0-1744664589297.png

You may also do without this index function if you replace calls like

Werner_E_1-1744664618865.png

by

Werner_E_2-1744664636110.png

but you would have to assure that this if-statement is not called for the last i because then there is no element # i+1

 

View solution in original post

5 REPLIES 5
Werner_E
25-Diamond I
(To:fahmed-2)

The vector "value" you create in the program does not get a value in each turn of the for loop.

I inserted a "try and catch" at the position where the error is thrown

Werner_E_0-1744663526976.png

and it can be seen by the result

Werner_E_1-1744663554858.png

that "value" has just 14 elements when i=25 and so you obviously can't access element #24.

 

You have to check your nested series of "if ... else if .... if ... else ... etc." to assure that "value" is assigned a value in every 'round' of the for-loop.

 

BTW, your "index" function could be written by simply using the "match" function. Only difference is the behaviour if the value (z) is not found in the vector.

Your function returns 0 in this case but "match" will return an error. If this is mandatory you may throw in a try and catch as shown:

Werner_E_0-1744664589297.png

You may also do without this index function if you replace calls like

Werner_E_1-1744664618865.png

by

Werner_E_2-1744664636110.png

but you would have to assure that this if-statement is not called for the last i because then there is no element # i+1

 

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you so much @Werner_E 

fahmed-2
13-Aquamarine
(To:fahmed-2)

one more question @Werner_E . with out using the "match" function, how can I locate the index number of an array based off a given number. In my case , I would like to locate the index number from "H_end.layer_vec.total" where the "H1.Active" number is situated. I tried with the following but not working. The reason I am hesitant to use match is because if the given number does not lie within "H_end.layer_vec.total" then it gives an error number. I like my sheet to be more universal in terms of the given 4 layers

 

fahmed2_0-1744668777252.png

 

Werner_E
25-Diamond I
(To:fahmed-2)

I am not sure what you would like to achieve.

What should be returned if the parameter z is not a value within the vector H.end.layer_vec.total.

The same applies to using "match" with the "try" bracket around.

 

In case you would rather like to return the index of  nearest value you may try the optional argument "near".!?

 

The reason your version does not seem to work is because of numeric precison / numeric round off error due to conversion m <-> ft

You can cope with that by not asking for exact equality but rather by asking if the difference is approximately zero.

Match seems to work OK, but you can use "near" to be on the safe side

Werner_E_0-1744691355206.png

 

You can see the numeric round-off error by evaluating the difference

Werner_E_1-1744691547927.png

 

fahmed-2
13-Aquamarine
(To:Werner_E)

Thank you @Werner_E 

Announcements

Top Tags