Build my own AND function

5 ビュー (過去 30 日間)
Thom
Thom 2017 年 4 月 20 日
回答済み: Roger Stafford 2017 年 4 月 20 日
Can someone help me with this exercise, I must implement a function wich works like a logical and operator but without using the and function.I have already wrote some code but, i don`t know how to implement if a&b=1 | a&b=0 without using the "and"
function [ A ] = AND( E_1,E_2 )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
a=logical(E_1); b=logical(E_2);
if (a~=b)
A=logical(0)
end
if(a==b)
A=logical(1)
end
end end

回答 (1 件)

Roger Stafford
Roger Stafford 2017 年 4 月 20 日
Your code doesn't achieve the 'and' function. In the case when both a and b are false, the valid 'and' result should be false, but in your case it is true. You can use the or '|' function:
A = ~(~a|~b);
In matlab you can take advantage of the numerical representation of true and false:
A = logical(a*b);

カテゴリ

Help Center および File ExchangeApp Building についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by