MATLAB 05-1 (Logic = boolean algebra)

신예진·2022년 1월 27일
0

목록 보기
9/17

Logic

1-1) relational operator

< : less than
<= : less than or equal to
> : greater than
>= : greater than or equal to
== : equal to
-= : not equal to    

1-2) relational operators

< <= == ~= > >=
numerical value (relational operator) numerical value
2 < 5
output logical value : 0(false), 1(true)

2-1) logical operator

& : and
| : or
~ : not
xor : exclusive or xor(a,b)

2-2) logical operators

& | ~ xor
logical value (logical operator) logical value
(x == 3) | (x < 0)
0 < x < 1 % try for x = 0.5 % 이때, 0 < x(=0.5) 는 true(=1)니까 1(논리값) < 1(숫자) 이 되어서 ans = 0(논리값)이 됨.
% 이걸 우리가 생각한 대로 쓰고 싶으면 (0 < x) & (x < 1)

3) how to give logical value

0 : false
any number other than 0 : true % -100 & 2000 을 입력하면 1을 반환한다. 왜? matlab은 0이 아닌 숫자를 true라고 인식.

Indexing Array with Logical values

4) Logical index

logical vector can be used as array index
only "true" elements are returned

5) Logical index Ex

a = rand(8,1);
i = ones(4,1)
j = logical(i) % i and j look the same (whos i j). but, it's not the same.
a(i)
a(j)
a = a(a > 0.5) % or two steps : i = a > 0.5, a(i)

A = rand(8,3)
A > 0.5
A(A > 0.5) % 이렇게 하면 dimension이 무시되고 일렬로 나와버림
A .* (A > 0.5) % 이렇게 하면 같은 dimension으로