ERROR: Array indices must be positive integers or logical values. (2024)

4 views (last 30 days)

Show older comments

Guillermo Pourreau Pons on 19 May 2023

  • Link

    Direct link to this question

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values

  • Link

    Direct link to this question

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values

Commented: Rik on 19 May 2023

Open in MATLAB Online

"Why am I getting the error 'Array indices must be positive integers or logical values' in MATLAB when using a for loop?"

% grupo 20

Km=1.5;

Ra=8;

La=0.03;

J=0.08;

Kg=10;

Kt=0.20;

Kv=40;

T=0.02;

syms k;

%F2 y F3

A=[0 1 0; 0 0 1; -Kg*Km^2 -Ra*J -La*J];

B=[0; 0; Km];

C=[1 0 0];

sys=ss(A,B,C,0)

sys = A = x1 x2 x3 x1 0 1 0 x2 0 0 1 x3 -22.5 -0.64 -0.0024 B = u1 x1 0 x2 0 x3 1.5 C = x1 x2 x3 y1 1 0 0 D = u1 y1 0 Continuous-time state-space model.

sys1=c2d(sys,T,'zoh')

sys1 = A = x1 x2 x3 x1 1 0.02 0.0002 x2 -0.0045 0.9998 0.02 x3 -0.45 -0.0173 0.9998 B = u1 x1 2e-06 x2 0.0003 x3 0.03 C = x1 x2 x3 y1 1 0 0 D = u1 y1 0 Sample time: 0.02 secondsDiscrete-time state-space model.

[V,D]=eig(sys1.A)

V =

-0.0618 - 0.0965i -0.0618 + 0.0965i -0.1235 + 0.0000i 0.1573 - 0.2876i 0.1573 + 0.2876i 0.3393 + 0.0000i 0.9378 + 0.0000i 0.9378 + 0.0000i -0.9325 + 0.0000i

D =

1.0265 + 0.0516i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.0265 - 0.0516i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.9465 + 0.0000i

V1=inv(V)

V1 =

-1.4158 + 2.4544i 0.4749 + 0.9156i 0.3602 + 0.0082i -1.4158 - 2.4544i 0.4749 - 0.9156i 0.3602 - 0.0082i -2.8476 + 0.0000i 0.9551 + 0.0000i -0.3478 + 0.0000i

Af=V*Dk*V1

Af=

ERROR: Array indices must be positive integers or logical values. (2)

X = zeros(3, 1);

y = zeros(25, 1);

for k=1:25

for j=1:K_sym

X(k)=Af(k)*X(0)+j*Af(K_sym)*B

y(k)=C*X(k)

end

end

Unrecognized function or variable 'K_sym'.

2 Comments

Show NoneHide None

Dyuman Joshi on 19 May 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752439

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752439

Open in MATLAB Online

I am not able to reproduce the same error you obtained.

However, there is another error, as you can see below, which occurs because you are trying to use a variable you have not defined.

% grupo 20

Km=1.5;

Ra=8;

La=0.03;

J=0.08;

Kg=10;

Kt=0.20;

Kv=40;

T=0.02;

syms k;

%F2 y F3

A=[0 1 0; 0 0 1; -Kg*Km^2 -Ra*J -La*J];

B=[0; 0; Km];

C=[1 0 0];

sys=ss(A,B,C,0);

sys1=c2d(sys,T,'zoh');

[V,D]=eig(sys1.A);

V1=inv(V);

Dk=D^k;

Af=V*Dk*V1;

X = zeros(3, 1);

y = zeros(25, 1);

for k=1:25

for j=1:K_sym

X(k)=Af(k)*X(0)+j*Af(K_sym)*B;

y(k)=C*X(k)

end

end

Unrecognized function or variable 'K_sym'.

Guillermo Pourreau Pons on 19 May 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752479

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752479

I'm sorry, the correct code is this one:

for k=1:25

for j=1:k-1

X(k)=Af(k)*X(0)+j*Af(K-1)*B

y(k)=C*X(k)

end

end

I continue getting the same error, and I dont know how to avoid X(0). Have you any idea?

Sign in to comment.

Sign in to answer this question.

Answers (1)

Rik on 19 May 2023

  • Link

    Direct link to this answer

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#answer_1239684

  • Link

    Direct link to this answer

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#answer_1239684

Actually, the result of your code is a missing variable. Once you fix that, you will have to edit your code to avoid X(0).

2 Comments

Show NoneHide None

Guillermo Pourreau Pons on 19 May 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752474

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752474

True, I'm sorry

The correct code is this one:

for k=1:25

for j=1:k-1

X(k)=Af(k)*X(0)+j*Af(K-1)*B

y(k)=C*X(k)

end

end

I continue getting the same error, and I dont know how to avoid X(0). Have you any idea?

Rik on 19 May 2023

Direct link to this comment

https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752729

  • Link

    Direct link to this comment

    https://www-ah.mathworks.com/matlabcentral/answers/1968019-error-array-indices-must-be-positive-integers-or-logical-values#comment_2752729

You can find an explanation for the layout tools here. It is the toolbar you see when you edit your question.

Since you didn't write any comments and only use single/two letter variable names, I have no clue what you want to do. So I have no idea what you should change. Currently you are trying to retrieve the zeroth element of X. Since that is before the first element, it doesn't exist.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Mathematics and OptimizationSymbolic Math ToolboxMathematicsNumber Theory

Find more on Number Theory in Help Center and File Exchange

Tags

  • error
  • arrays

Products

  • MATLAB

Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


ERROR: Array indices must be positive integers or logical values. (8)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

ERROR: Array indices must be positive integers or logical values. (2024)

FAQs

What does array indices must be positive, integers, or logical values? ›

Expert-Verified Answer

This means that the first element of an array has an index of 1, not 0 as in some other programming languages. When accessing elements of an array, one must use positive integers to specify the position of the element within the array.

Do Matlab array indexes have to be positive integers? ›

Array indices must be positive integers or logical values.

Which indices must either be real positive integers or logicals? ›

This error occurs when you attempt to index into an array using indices that are not positive integers or logical values. Here are some tips for common situations that cause this error message: 1) Double check that your indices are positive integers. Indices in MATLAB cannot be 0, and by default, start with 1.

Is the index of an array must be a positive integer greater than zero True or false? ›

An index within an array is always a positive integer. Most modern languages will not allow a negative index and will generate an outside the bounds of the array error.

What are positive integer indices? ›

Indices are a way of writing numbers in a more convenient form. The index or power is the small, raised number next to a normal letter or number. It represents the number of times that normal letter or number has been multiplied by itself, for example: a2 = a × a.

What is an array of indices? ›

In Python, indexing in arrays works by assigning a numerical value to each element in the array, starting from zero for the first element and increasing by one for each subsequent element. To access a particular element in the array, you use the index number associated with that element.

What is a logical value in MATLAB? ›

MATLAB® represents Boolean data using the logical data type. This data type represents true and false states using the numbers 1 and 0 , respectively. Certain MATLAB functions and operators return logical values to indicate fulfillment of a condition.

How do you find the positive number in an array? ›

Using for Loop

In this approach, we are using the for loop to iterate over each element of the array and check if each element is greater than 0, which means a positive number.

Are arrays always integers? ›

Array indexes are always integers, because at the lowest level, arrays are contiguous blocks of memory, and the array is simply represented by a pointer to its first element. The program has to perform numeric addition to this pointer to retrieve the successive element from the allocated memory.

What is the best positive integer? ›

So, the positive integers list is 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ..... and so on. 1 is the smallest positive integer and the greatest positive integer is not known as the list is endless. ► Also Check: Integers.

Is it true that every integer is either positive or negative? ›

The statement `` All integers are positive or negative '' is a true statement . This is because integers are whole numbers that can be either positive , negative , or zero . Therefore , all integers fall under one of these categories .

Can you give an integer which is neither positive or negative? ›

Zero is the integer denoted 0 that, when used as a counting number, means that no objects are present. It is the only integer (and, in fact, the only real number) that is neither negative nor positive.

Is array index based on integer value? ›

Arrays must be indexed by int values; short, byte, or char values may also be used as index values because they are subjected to unary numeric promotion (§5.6. 1) and become int values.

What happens if array index is negative? ›

Negative indexing allows accessing array elements relative to the end of the array rather than the beginning. For example, in Python, index -1 refers to the last element, -2 refers to second last, and so on.

Should array indices start at 0 or 1? ›

In computer science, array indices usually start at 0 in modern programming languages, so computer programmers might use zeroth in situations where others might use first, and so forth.

Can array indexes be negative? ›

In many programming languages (eg: JavaScript, Python, Java, C#, Clojure, etc.) you can use a negative index to select items in arrays or characters in text counting from the end of the array or string. Positive indexes count from the start. Negative indexes count from the end.

What are the integer values in an array? ›

Arrays in C are a collection of values that store items of the same data type – an integer array holds only elements of the type int , a float array holds only elements of the type float , and so on.

Top Articles
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6531

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.