MATLAB Answers

# Output argument ‹variable› (and maybe others) not assigned during call to ‹function›

13 views (last 30 days)
Jonas Damsbo on 9 Jan 2020
Answered: Allen on 9 Jan 2020
When I run my script, I get this error from my function. I can't see why
This is my function:
function pdf = apost(m);
sd1 = 300^2;
A = sd1*ones(1,12);
C_m = diag(A);
Cm = inv(C_m);
sd2 = 1;
B = sd2*ones(1,12);
C_d = diag(B);
Cd = inv(C_d);
[N,~] = size(m);
if N==5
Cm = inv(C_m);
Cd = inv(C_d);
m0 = [210.540977721001,336.865564353602,437.925233659682,516.527198675522,572.671459401123,599.339983245783,595.129163691363,574.075065919263,523.545231266222,442.136053214102,305.986220954521,179.661634321921];
g = [-5.57961065006546,-9.24249212739290,-13.3900010001821,-17.3638621584906,-20.6030599093808,-22.2586614660132,-21.9922416252264,-20.6883184326565,-17.7504996477641,-13.5862938793517,-8.19100512552534,-4.94137181487074]
y = [-15.0 -24.0 -31.2 -36.8 -40.8 -42.7 -42.4 -40.9 -37.3 -31.5 -21.8 -12.8];
L = (y-g)'.*C_d.*(y-g);
rho = (m-m0)'.*Cm.*(m-m0);
sigma = -0.5.*(rho + L);
else
disp('Incorrect dimension of input vector')
end
This is my script:
% (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) = apost(pts(:,1)); %Compute function in starting point
step = 0.5; %Set step length
return
%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

#### 4 Comments

Show 1 older comment
Jonas Damsbo on 9 Jan 2020
Yes, I can see that when I run my script, I now got the error:
"Unable to perform assignment because the left and right sides have a different number of elements."
For this line:
hfuncval(1) = apost(pts(:,1));
How can I fix that?
My hfuncval is 1x10000, m0 is 1x12 and pts is 12x10000
Guillaume on 9 Jan 2020
For the line
hfuncval(1) = apost(pts(:,1)
which assigns a value to the scalar hfuncval, the function apost must return a scalar.
The error tells you it doesn't (I suspect it returns a vector the same size as pts(:, 1) so 12x1, but we don't know since you haven't shown us what pdf is).
You need to modify apost so it returns a scalar even when it gets a vector as input, or change that line so it doesn't attempt to store a vector in a scalar.
Rik on 9 Jan 2020
Just as a side note: this question is related to the previously posted question.

Sign in to comment.

### Answers (1)

Allen on 9 Jan 2020
Currently, you have only decleared pdf as an output argument at the start of your function, but have neglected to assign a value to it within the function. You need to define pdf somewhere within your function.
function pdf = apost(m);
sd1 = 300^2;
A = sd1*ones(1,12);
C_m = diag(A);
Cm = inv(C_m);
sd2 = 1;
B = sd2*ones(1,12);
C_d = diag(B);
Cd = inv(C_d);
[N,~] = size(m);
if N==5
Cm = inv(C_m);
Cd = inv(C_d);
m0 = [210.540977721001,336.865564353602,437.925233659682,516.527198675522,572.671459401123,599.339983245783,595.129163691363,574.075065919263,523.545231266222,442.136053214102,305.986220954521,179.661634321921];
g = [-5.57961065006546,-9.24249212739290,-13.3900010001821,-17.3638621584906,-20.6030599093808,-22.2586614660132,-21.9922416252264,-20.6883184326565,-17.7504996477641,-13.5862938793517,-8.19100512552534,-4.94137181487074]
y = [-15.0 -24.0 -31.2 -36.8 -40.8 -42.7 -42.4 -40.9 -37.3 -31.5 -21.8 -12.8];
L = (y-g)'.*C_d.*(y-g);
rho = (m-m0)'.*Cm.*(m-m0);
sigma = -0.5.*(rho + L);
%pdf = 'Assign value during this case'
else
disp('Incorrect dimension of input vector')
%pdf = 'This case might require a different value for pdf. eg - pdf = [];
end
end

#### 0 Comments

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!

Translated by