调用期间未分配输出参数:Matlab
Posted
技术标签:
【中文标题】调用期间未分配输出参数:Matlab【英文标题】:Output Argument Not Assigned During Call: Matlab 【发布时间】:2020-09-28 01:48:15 【问题描述】:我正在尝试使用 LCG 为一个项目生成 2500 个伪随机数。但是,当我尝试运行代码时,我不断收到错误“在调用 lcgg 期间未分配输出参数‘p’(可能还有其他参数)。”。我希望有人能帮助我理解为什么 p 不在输出中以及如何解决这个问题?
clear;
clc;
M = 2500;
ID = 801201076;
disp('N = '); disp(mod(ID,3));
[A,p1] = lcgg(M,30269,171,0,1);
[B,p2] = lcgg(M,30307,172,0,1);
[C,p3] = lcgg(M,30323,170,0,1);
disp('Period = '); disp(p2);
% Combine the 3 generators as in Wichmann and Hill
figure(1);
subplot(2,1,1);hist(B);title('Histogram for Uniform RDN from LCG');
subplot(2,1,2);qqplot(rand(300,1),B);title('QQplot for uniform RDN from LCG');
figure(2);
scatter(B(1:(M-1),1),B(2:M,1),4);title('Plot of sequential pairs for LCG');
D = A + B + C - fix(A + B + C); % internal Matlab uniform random number generator
u = rand(M,1); % internal Matlab uniform random number generator
figure(3);
subplot(2,1,1);scatter(u(1:(M-1),1),u(2:M,1),4);title('Plot of Sequential Pairs for Matlab Internal Generator');
subplot(2,1,2);scatter(D(1:M-1),1),D(2:M,1),4;title('Plot of sequential pairs for 3 LCG Combined')
% Calculate the period
i = 1;
j = 2;
while A(i,1) ~= A(j,1) && i < m
if j < m
j = j+1;
else
i = i+1;
j = j+1;
end
end
if i == m
p = m;
else
p = j-1;
end
A = A./m;
if M <= m
A = A(1:M,:);
end
function[A,p] = lcgg(M,m,a,c,x0)
% Generates a matrix of random numbers using lcg
% Calculate the period
% Input: M: total number of random numbers needed
% m, a, x, x0
% Output: A: M * 1 matrix of random numbers
% p: period of the LCG random number generator
A = zeros(m,1);
for i = 1:m
if i == 1
A(i,1) = lcg(m,a,c,x0);
else
A(i,1) = lcg(m,a,c,A(i-1,1));
end
end
end
% The LCG Function:
function[x] = lcg(m,a,c,x0)
% Linear Congruential Generator (LCG)
x = mod(a*x0+c, m);
end
【问题讨论】:
【参考方案1】:你定义一个函数:
function[A,p] = lcgg(...)
在函数体中,您需要为A
和p
这两个输出变量赋值。您没有将任何内容分配给 p
,因此是消息。
【讨论】:
啊,谢谢!一旦我将周期计算移到 lcgg 函数中,它就起作用了。以上是关于调用期间未分配输出参数:Matlab的主要内容,如果未能解决你的问题,请参考以下文章