[初学笔记] matlab 中 all, any, strcmp, struct, while loop


1  首先熟悉 strcmp 这个函数

tf = strcmp (s1,s2)

输入
s1 = 'upon';
s2 = { 'Once', 'upon';
      'a', 'time'};
tf = strcmp(s1,s2)


会得到
tf = 2×2 logical array
   0   1
   0   0

2  好的下面是 all 和 any的用法,详情自己看这个blog

转自 http://www.matlabsky.com/thread-4150-1-1.html


3 用上 struct,以及在循环 while loop里面,减少逻辑运算符号的使用
我们可以这样做
下面是我自己写的代码,亲测可以用

you.gender = input ('\n\n please enter your gender. (f/m/x)\n\n','s');
input1 = 'f';input2 = 'm';input3 = 'x'; % define the input answer
s1 = {input1,input2,input3}; % 3 input strings into the cell
tf = strcmp(you.gender,s1); % compare the input and the right answer
while any (tf) == 0 % if no correct answer(1),then input again
    fprintf('\n\n Error! Invalid input!\n\nPlease enter ''f'' for female, ''m'' for male, ''x'' for third sex.\r\n');
    you.gender = input ('\n\n please enter your gender. (f/m/x)\n\n','s');
end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值