1vue (au cours des 30derniers jours)
Afficher commentaires plus anciens
Jonas Damsbo le 8 Jan 2020
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:
6commentaires Afficher 4 commentaires plus anciensMasquer 4 commentaires plus anciens
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
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
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
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"
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_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
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
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
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
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
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
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
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
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
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
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
So the theoy of the algorithm is:
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
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
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.
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