1、错误使用continue于break,导致‘索引超出矩阵维度’
if i <= r && j<=r
for p = 1:i+5
for q = 1:j+5
Column_S = size(S_position,2);
for k = 1:Column_S%k represent the column of vector
% 'S_position' Find the proper position.
if k>size(S_position,2)
fprintf('%f\n',k)
fprintf('%f\n',size(S_position,2))
end
if S_position(1,k)-p==0 && S_position(2,k)-q == 0
L = max(abs(i-p), abs(j-q));
new_M(p,q) = new_M(p,q) + ...
(1- (1/(1+exp(-L)))) *rand*rand*rand;
if new_M(p,q)>=lambda && rand>=0.5
I_position = [I_position,S_position(:,k)];
S_position(:,k) = [];
end
break;%If find the position, then end the loop.
end
end
end
end
【代码摘自元胞自动机】
分析:此程序的特点是,在for k 循环之后会有矩阵‘S_position’的纬度减小(S_position(:,k) = [];),然而Column_S = size(S_position,2);k = 1:Column_S,导致‘索引超出矩阵维度’的错误。
2、 disp函数的使用
name = 'Alice';
age = 12;
X = [name,' will be ',num2str(age),' this year.'];
disp(X)
Alice will be 12 this year.
本文探讨了在元胞自动机程序中因错误使用continue与break导致的索引超出矩阵维度的问题,分析了原因并提出了解决方案。同时,介绍了如何正确使用disp函数显示字符串与数值组合的信息。
938

被折叠的 条评论
为什么被折叠?



