台湾大学系列 MATLAB学习笔记(二) 编程

创建分节符:%%,执行程序的时候每个节独立运行
断点调试程序

Relation opearters关系逻辑运算符
<    <=    >    >=    ==    ~=    
&& and 
|| or

logic shrtcut逻辑短路
逻辑短路与“与”操作: 在一个“与”操作中,如果第一个条件为假(False),那么整个表达式的值就会是假,而无论后面的条件是什么,后面的条件将不再被计算。
逻辑短路与“或”操作: 在一个“或”操作中,如果第一个条件为真(True),那么整个表达式的值就会是真,而无论后面的条件是什么,后面的条件将不再被计算。


a=30;
b=4;
c=2;

(a>b)||(b==2)
(a>b)&&(b==2)

a=[1 2 3]
b=[0 2 3]
a&b
||用于逻辑标量
|用于逻辑标量,也可用于数组value对比

if elseif else

a=3
if rem(a,2)==0
    disp("a is even")
elseif rem(a,2)~=0
    disp('a is odd')
else 
    disp('也可以不写elseif在这个例子中')
end

for

for n=1:10
    a(n)=2^n;%直接创建数组索引
end
disp('a array is')
disp(a)


for例子2 

tic
toc
计时器函数

tic
A=zeros(10,10)
for ii=1:size(A,1);
    for jj=1:size(A,2);
        A(ii,jj)=ii+jj;
    end
end
A
toc

while

help prod;
n=1
while prod(1:n)<1e100
    n=n+1
end
1e100=1*10^(100)
prod(1:n)=n!

switch case otherwise

input_num=7;
switch input_num
    case 1
        disp('1')
    case 2
        disp('2')
    case 7
        disp('7 haha')
    otherwise
        disp('no at all')
        
end

try catch 略

break    terminate the loop of for or while


与while/for有关伪代码:
while exxor?error_threshold
    if k>100
        break
        x=x-sin(x)
        k=k+1
    end
end

continue  

 
pass control to next iteration loop of for or while

其他

end    [1]last array index [2]terminate the block

pause    halt execution temporarily
return    return control to invoking function

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值