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

1vue (au cours des 30derniers jours)

Afficher commentaires plus anciens

Jonas Damsbo le 8 Jan 2020

  • Lien

    Utiliser le lien direct vers cette question

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values

  • Lien

    Utiliser le lien direct vers cette question

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values

Commenté: the cyclist le 9 Jan 2020

Ouvrir dans MATLAB Online

I have this MatLab code where I want to run a metropolis algorithm.

In my s rho is a matrix 12x12 and L is a matrix 12x12. m0 is a vector 1x12.

When I run my code I got the error 'Array indices must be positive integers or logical values' in hfuncval(1) where I want to compute s in the starting point.

How can come throught that?

Thank

s = -0.5.*(rho + L);

% (4) Metropolis algorithm

K = 10000; %Number of samples

pts = zeros(length(m0),K); %Array with sample output points

hfuncval = zeros(K); %Array with function value outputs

pts(:,1) = m0; %Set starting points

hfuncval(1) = s(pts(:,1)); %Compute function in starting point

step = 0.5; %Set step length

%Start sampling

for k = 1:K

[ptpert] = pts(:,k-1) + (2*rand(2,1)).*step; % propose perturbed point

hfuncpert = hfunc(ptpert); % Compute function in perturbed point

u = rand; % Generate random number in [0,1]

if u < hfuncpert/hfuncval(k-1) % accepting new points

% if u < exp(log(hfuncpert)-log(hfuncval(k-1)))

pts(:,k) = ptpert;

hfuncval(k) = hfuncpert;

else % Rejecting new points

pts(:,k) = pts(:,k-1);

hfuncval(k) = hfuncval(k-1);

end

end

The theory of the algorithm is:

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

6commentaires

Afficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens

Rik le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784307

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784307

What is the value of m0? That call should also return a vector, which will not fit in your scalar index. And do you intend your output matrix to be K by K? That seems very large, especially for code that you are currently writing.

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784310

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784310

Sorry, I forgot that. m0 is a vector 1x12

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784311

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784311

Ouvrir dans MATLAB Online

But, it is in the line

hfuncval(1) = s(pts(:,1));

I got the error "Array indices must be positive integers or logical values"

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784418

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784418

You're using the values in the first column of pts as indices into s. So m0 contains one or more values you can't use as indices. Once you fix that you will notice this line is trying to store 12 values to a single position. Note that s is not a function, it is an array.

Jonas Damsbo le 9 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784438

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784438

Ouvrir dans MATLAB Online

Okay, now I have doing this.

s = @(rho,L) -0.5.*(rho + L);

rho = (m-m0)'.*Cm.*(m-m0);

L = (y-g)'.*C_d.*(y-g);

sigma = s(rho,L);

% (4) Metropolis algorithm

K = 10000; %Number of samples

pts = zeros(length(m0),K); %Array with sample output points

hfuncval = zeros(1,K); %Array with function value outputs

pts(:,1) = m0; %Set starting points

hfuncval(1) = sigma(1,1); %Compute function in starting point

step = 0.5; %Set step length

%Start sampling

for k = 2:K

ptpert = pts(:,k-1) + (2*rand(12,1)-1)*step; % propose perturbed point

hfuncpert = sigma(ptpert); % Compute function in perturbed point

u = rand; % Generate random number in [0,1]

if u < hfuncpert/hfuncval(k-1) % accepting new points

% if u < exp(log(hfuncpert)-log(hfuncval(k-1)))

pts(:,k) = ptpert;

hfuncval(k) = hfuncpert;

else % Rejecting new points

pts(:,k) = pts(:,k-1);

hfuncval(k) = hfuncval(k-1);

end

end

But I have again the error 'Array indices must be positive integers or logical values.'

Now in this line:

hfuncpert = sigma(ptpert);

Rik le 9 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784440

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784440

Look at the values of ptpert at that point. Use the debugging tools to step through your code line by line.

Connectez-vous pour commenter.

Connectez-vous pour répondre à cette question.

Réponses (1)

the cyclist le 8 Jan 2020

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#answer_409287

  • Lien

    Utiliser le lien direct vers cette réponse

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#answer_409287

Ouvrir dans MATLAB Online

In this line:

hfuncval(k) = hfuncval(k-1);

in the first iteration of the for loop, k == 1, so you are attempting to access the "zeroth" element of the array hfuncval, which does not exist.

9commentaires

Afficher 7 commentaires plus anciensMasquer 7 commentaires plus anciens

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784314

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784314

Ouvrir dans MATLAB Online

Thats works, thanks!

But now I got a new error:

"Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error.

To construct matrices, use brackets instead of parentheses."

In this line:

[ptpert] = pts(:,k-1) + (2*rand(2,1)1)*step;

Can you see why?

the cyclist le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784316

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784316

Ouvrir dans MATLAB Online

This is not valid MATLAB syntax:

(2*rand(2,1)1)

I'm not sure what you intended there, so I can't offer a solution.

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784321

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784321

So, I want to make a perturbed point in that line so I can make a new randomly guess of the modelparameters which is the function s.

the cyclist le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784326

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784326

Ouvrir dans MATLAB Online

What did you expect from the line of code I pasted, specifically?

Maybe you intended

(2*rand(2,1)-1)

and left out the minus sign?

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784329

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784329

Ouvrir dans MATLAB Online

I got the error 'Array indices must be positive integers or logical values.' in this line again

hfuncval(1) = s(pts(:,1));

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784330

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784330

But I expect a randomly generated vector column.

Jonas Damsbo le 8 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784344

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784344

So the theoy of the algorithm is:

Array indices must be positive integers or logical values (17)

Jonas Damsbo le 9 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784441

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784441

Ouvrir dans MATLAB Online

Okay so, I have changed a little bit in my code:

s = @(rho,L) -0.5.*(rho + L);

rho = (m-m0)'.*Cm.*(m-m0);

L = (y-g)'.*C_d.*(y-g);

sigma = s(rho,L);

% (4) Metropolis algorithm

K = 10000; %Number of samples

pts = zeros(length(m0),K); %Array with sample output points

hfuncval = zeros(1,K); %Array with function value outputs

pts(:,1) = m0; %Set starting points

hfuncval(1) = sigma(1,1); %Compute function in starting point

step = 0.5; %Set step length

%Start sampling

for k = 2:K

ptpert = pts(:,k-1) + (2*rand(12,1)-1)*step; % propose perturbed point

hfuncpert = sigma(ptpert); % Compute function in perturbed point

u = rand; % Generate random number in [0,1]

if u < hfuncpert/hfuncval(k-1) % accepting new points

% if u < exp(log(hfuncpert)-log(hfuncval(k-1)))

pts(:,k) = ptpert;

hfuncval(k) = hfuncpert;

else % Rejecting new points

pts(:,k) = pts(:,k-1);

hfuncval(k) = hfuncval(k-1);

end

end

But I have again the error 'Array indices must be positive integers or logical values.'

Now in this line:

hfuncpert = sigma(ptpert);

the cyclist le 9 Jan 2020

Utiliser le lien direct vers ce commentaire

https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784543

  • Lien

    Utiliser le lien direct vers ce commentaire

    https://fr.mathworks.com/matlabcentral/answers/499516-array-indices-must-be-positive-integers-or-logical-values#comment_784543

Ouvrir dans MATLAB Online

I have to admit I have not reviewed your code closely.

But the error makes it pretty clear, right? ptpert is not a positive integer, so it cannot be used as an index into a vector. For example, if ptpert is 1.5, what does sigma(1.5) represent?

It looks like the line

ptpert = pts(:,k-1) + (2*rand(12,1)-1)*step;

is where you go wrong. That doesn't look like it is going to be an integer.

Connectez-vous pour commenter.

Connectez-vous pour répondre à cette question.

Voir également

Catégories

SciencesPhysicsQuantum Mechanics

En savoir plus sur Quantum Mechanics dans Help Center et File Exchange

Tags

  • array
  • metropolis
  • indicies
  • algorithm

Community Treasure Hunt

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

Start Hunting!

Une erreur s'est produite

Impossible de terminer l’action en raison de modifications de la page. Rechargez la page pour voir sa mise à jour.


Translated by Array indices must be positive integers or logical values (20)

Array indices must be positive integers or logical values (21)

Sélectionner un site web

Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .

Vous pouvez également sélectionner un site web dans la liste suivante :

Amériques

  • 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)

Asie-Pacifique

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

Contactez votre bureau local

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

FAQs

Array indices must be positive integers or logical values? ›

Expert-Verified Answer

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

It means that you are trying to access an element in an array using an index value that is not a positive integer or a logical value. In most programming languages, array indices start from 1 or 0, and thus you must use an integer value within this range.

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 0 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.

Are indexes always integers? ›

Yes, it has to be an integer because you are essentially performing pointer arithmetic where &array[0] is a pointer to the beginning of the array (well, technically it has to be an integer because that's what the spec says, but this is why).

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.

Can MATLAB array indexes be negative? ›

Notes: The array should be initialize to be as large as the largest positive index, - the smallest negative (most negative) index.

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 .

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.

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.

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.

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.

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.

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.

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: Chrissy Homenick

Last Updated:

Views: 6533

Rating: 4.3 / 5 (54 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Chrissy Homenick

Birthday: 2001-10-22

Address: 611 Kuhn Oval, Feltonbury, NY 02783-3818

Phone: +96619177651654

Job: Mining Representative

Hobby: amateur radio, Sculling, Knife making, Gardening, Watching movies, Gunsmithing, Video gaming

Introduction: My name is Chrissy Homenick, I am a tender, funny, determined, tender, glorious, fancy, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.