F**k Matlab in daily life

本文探讨了在元胞自动机程序中因错误使用continue与break导致的索引超出矩阵维度的问题,分析了原因并提出了解决方案。同时,介绍了如何正确使用disp函数显示字符串与数值组合的信息。

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值