본문으로 건너뛰기
Hub Nexus
업데이트됨

작성자아직 작성자가 없습니다맡기

개선할 점이 보이나요? 변경을 제안하세요.

후원

이 페이지는 공개 상태입니다.

Introduction

A large number of MATLAB's functions are operations on two types of numbers: rational numbers and boolean numbers.

Rational numbers are what we usually think of when we think of what a number is. 1, 3, and -4.5 are all rational numbers. MATLAB stores rational numbers as doubles by default, which is a measure of the number of decimal places that are stored in each variable and thus of how accurate the values are. Note that MATLAB represents irrational numbers such as pi with rational approximations, except when using the symbolic math toolbox. See that section for details.

Boolean numbers are either "TRUE" or "FALSE", represented in MATLAB by a 1 and a 0 respectively. Boolean variables in MATLAB are actually interchangable with doubles, in that boolean operators can be performed with arrays of doubles and vice versa. Any non-zero number in this case is considered "TRUE".

Most of the rational operators also work with complex numbers. Complex numbers; however, cannot be interchanged with boolean values like the real rationals can.

Note: MATLAB refers to Booleans as "logicals" and does not use the word "Boolean" in code or documentation.

Rational Operators on Single Values

MATLAB has all the standard rational operators. It is important to note, however, that Unless told otherwise, all rational operations are done on entire arrays, and use the matrix definitions. Thus, even though for now we're only talking about operations on a single value, when we get into arrays, it will be important to distinguish between matrix and component-wise multiplication, for example:
Add, Subtract, Multiply, Divide, Exponent operators.

%addition
a = 1 + 2

%subtraction
b = 2 - 1

%matrix multiplication
c = a * b

%matrix division (pseudoinverse)
d = a / b

%exponentiation
e = a ^ b

The modulo function returns the remainder when the arguments are divided together, so a modulo b means the remainder when a is divided by b.

%modulo
remainder = mod(a,b)

All of these functions except for the modulus work for complex numbers as well.

Relational Operators

Equality '==' returns the value "TRUE" (1) if both arguments are equal. This must not be confused with the assignment operator '=' which assigns a value to a variable.

>> %relational
>>a=5;b=5;
>>a==b
ans =
logical
1
%Assignment
>>a=5;b=3;
>>a=b
a = 3

Note that in the first case, a value of 1 (true) is returned, however for the second case a gets assigned the value of b.

Greater than, less than and greater than or equal to, less than or equal to are given by >, <, >=, <= respectively. All of them return a value of true or false.
Example:

>>a=3;b=5;
>>a<=b
ans = 1
>>b<a
ans = 0

Boolean Operators on Single Values

The boolean operators are & (boolean AND) | (boolean OR) and ~ (boolean NOT /negation). A value of zero means false, any non-zero value (usually 1) is considered true.

Here's what they do:

>>%boolean AND
>> y = 1 & 0
y = 0
>> y = 1 & 1
y = 1
>>%boolean OR
>> y = 1 | 0
y = 1
>> y = 1 | 1
y = 1

The negation operation in MATLAB is given by the symbol ~, which turns any FALSE values into TRUE and vice versa:

>> c = (a == b)
c = 1
>> ~c
ans = 0

This is necessary because conditionals (IF/SWITCH/TRY) and loops (FOR/WHILE) always look for statements that are TRUE, so if you want it to do something only when the statement is FALSE you need to use the negation to change it into a true statement.

The NOT operator has precedence over the AND and OR operators in MATLAB unless the AND or OR statements are in parenthesis:

>> y = ~1 & 0
y = 0
>> y = ~(1&0)
y = 1

References

[1]

Where this page came from

This page was imported from Wikibooks. From “MATLAB Programming” on Wikibooks, by its contributors, under CC BY-SA 4.0. Changed here: set as a page; navigation and edit links left out; each image under its own licence, credited in its caption.

Nobody has written it yet — it is the source material at a new address, which is why search engines are asked to skip it and why no one earns from it. It is up for grabs: take it on, and it is yours to rewrite and to earn from.

이 페이지가 속한 매거진MATLAB Programming

언어English

이용 허락: CC BY-SA 4.0 · 출처 en.wikibooks.org

견해

코멘트

Spinner Logo
버전: 2CC0 1.0 (퍼블릭 도메인)
The runaway star that left the Tarantula Nebula
버전: 2CC0 1.0 (퍼블릭 도메인)
The Eagle Nebula, seen in the infrared
버전: 2CC0 1.0 (퍼블릭 도메인)
The Cosmic Cliffs are not cliffs
버전: 2CC0 1.0 (퍼블릭 도메인)
The recommended targets are real, but the largest health gain is between doing nothing and doing a little. That is the part worth knowing if you are starting from nothing.
버전: 2CC0 1.0 (퍼블릭 도메인)
Sleep is not one state but a cycle through several, each doing different work. Most sleep disorders are a failure to complete that cycle rather than a shortage of hours.
버전: 2CC0 1.0 (퍼블릭 도메인)
Four nebulae that revealed the shape of our galaxy