简化if循环具有多个条件 [英] simplifying if loops with multiple conditions

查看:102
本文介绍了简化if循环具有多个条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环:

if A == 1
    A = 2;
end
if B == 1
    B = 2;
end
if C == 1
    C = 2;

等... 现在这当然要花很多时间写,但是如果我这样写的话:

etc... Now this of course takes ages to write, but if I write it like:

if A == 1 || B == 1 || C == 1
    A = 2; B = 2; C = 2;
end

如果其中任何一个等于"1",它将更改所有参数 我想要的是一个简单的循环,在这里我可以说,如果A或B或C或...等于'X',则仅等于'X'的那个值的新值会更改为新值,不必像在第一个示例中那样编写它. 谢谢

it will change all of the parameters if any of them is equal '1' What I'd like is a simple loop where I can say that if A or B or C or... is equal 'X' than the new value of only the one that is equal to 'X' changes to the new value, without having to write it like I did in the first example. Thank you

推荐答案

也许如果将它们分组在矢量中,则可以执行以下操作:

Maybe if you group them in a vector, you can do:

params = [A B C];
params(params==1) = 2;

这篇关于简化if循环具有多个条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆