Array indices must be positive integers or logical values

조회 수: 8 (최근 30일)
Jonas Damsbo
Jonas Damsbo 2020년 1월 8일
댓글: the cyclist 2020년 1월 9일
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:
a.png
  댓글 수: 6
Jonas Damsbo
Jonas Damsbo 2020년 1월 9일
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
Rik 2020년 1월 9일
Look at the values of ptpert at that point. Use the debugging tools to step through your code line by line.

댓글을 달려면 로그인하십시오.

답변 (1개)

the cyclist
the cyclist 2020년 1월 8일
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.
  댓글 수: 9
Jonas Damsbo
Jonas Damsbo 2020년 1월 9일
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
the cyclist 2020년 1월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Quantum Mechanics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by