matlab while循环多个条件
matlab while loop multiple conditions
大家好,这里是 Matlab 编程,出于某种原因,我的 while 循环中不断出现错误。谁能给我一个关于如何在 while 循环中创建多个条件的示例?这是我的 while 循环,
while (user_input ~= 256);%try catch does not work very well for this
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
我希望它是这样的,
while (user_input ~= 256 || user_input ~= 128 || user_input ~= 64)
感谢您的帮助!
符号&
是and
逻辑运算符。您可以在 while
循环中将其用于多个条件。
while (user_input ~= 256 & user_input ~= 128 & user_input ~= 64)
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
正如烧杯指出的那样,您要求的是要求输入,只要它不是以下值之一:256、128 或 64。使用 or
逻辑运算符意味着 user_input
应该同时为 256、128 和 64 以打破循环。
您也可以使用ismember
。
conditionnal_values = [256, 128 , 64]
while ~ismember(user_input, conditionnal_values)
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
Luis Mendo 提出的另一种方法是使用 any
conditionnal_values = [256, 128 , 64]
while ~any(user_input==conditionnal values)
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
user_input == conditionnal_value
returns 由 1 和 0 组成的数组,取决于 conditionnal_values
的值是否与 user_input
匹配。然后 any 查找此数组上是否至少有一个 1
。然后我们应用 ~
,这是 not
运算符。
大家好,这里是 Matlab 编程,出于某种原因,我的 while 循环中不断出现错误。谁能给我一个关于如何在 while 循环中创建多个条件的示例?这是我的 while 循环,
while (user_input ~= 256);%try catch does not work very well for this
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
我希望它是这样的,
while (user_input ~= 256 || user_input ~= 128 || user_input ~= 64)
感谢您的帮助!
符号&
是and
逻辑运算符。您可以在 while
循环中将其用于多个条件。
while (user_input ~= 256 & user_input ~= 128 & user_input ~= 64)
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
正如烧杯指出的那样,您要求的是要求输入,只要它不是以下值之一:256、128 或 64。使用 or
逻辑运算符意味着 user_input
应该同时为 256、128 和 64 以打破循环。
您也可以使用ismember
。
conditionnal_values = [256, 128 , 64]
while ~ismember(user_input, conditionnal_values)
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
Luis Mendo 提出的另一种方法是使用 any
conditionnal_values = [256, 128 , 64]
while ~any(user_input==conditionnal values)
prompt = 'Please enter one of the listed gray levels (256, 128, 64, 32, 16, 8, 4, 2). ';
user_input = input(prompt);
end
user_input == conditionnal_value
returns 由 1 和 0 组成的数组,取决于 conditionnal_values
的值是否与 user_input
匹配。然后 any 查找此数组上是否至少有一个 1
。然后我们应用 ~
,这是 not
运算符。