请帮我修订一下一个很简单的Matlab程序

来源:百度知道 编辑:UC知道 时间:2024/05/24 05:02:10
这个程序所要实现的功能是输入X1和X2是否为真,并且选择采取哪种逻辑运算,输出真值运算结果。
我没有接触过Matlab,这是我一边查课本一边编出来的程序,思路不知道有没有问题,反正格式是一定有问题的。自己的思路如下:首先判定X1和X2输入的是true还是false,同时也要考虑X2可能是空值,即逻辑运算为NOT的情况。然后通过使用strcmpi的语句将X1与X2和"true"比较,如果相同就给将值1返回,不同则将值0返回,分别赋给变量a(对应X1)和b(对应X2)。然后再通过比较输入的运算方式的值来选择对a和b采取何种逻辑计算,并将计算所得结果赋予变量c。最后,如果c的值为1则程序显示结果为'true',为0则显示'false'。
请帮我修订一下这个程序,使之能够正常运行,万分感谢!
function logicaloperation(X1,X2,OP)
X1=input('Is X1 true of false?["true"/"false"]','s');
a=strcmpi(X1,'true');
%Get to know whether X1 is true of false
X2=input('Is X2 true of false?["true"/"false"/" "]','s');
if isempty(X2)
END
else b=strcmpi(X2,'true');
%Get to know whether X2 is true of false
OP=input('What is the operation?["and"/"or"/"nand"/"xor"/"not"]','s');
if OP=='and'

我没分析你的构思,只是改了你的程序,现在可以用了。主要错误是:
1。语法错误,缺少很多end。2。大小写不分,比如开始用大写OP,后面用小写op。3。xor函数使用错误。4。matlab里没有nand函数。

运行结果是
Is X1 true of false?["true"/"false"]true
Is X2 true of false?["true"/"false"/" "]false
What is the operation?["and"/"or"/"nand"/"xor"/"not"]xor
true

程序是
function baidu_logicaloperation
X1=input('Is X1 true of false?["true"/"false"]','s');
a=strcmpi(X1,'true');
%Get to know whether X1 is true of false
X2=input('Is X2 true of false?["true"/"false"/" "]','s');
if isempty(X2)
X='true';
else b=strcmpi(X2,'true');
%Get to know whether X2 is true of false
op=input('What is the operation?["and"/"or"/"nand"/"xor"/"not"]&#