
- •Matlab r2013a стр. 225
- •Continue Long Statements on Multiple Lines
- •Creating and Concatenating Matrices
- •Overview
- •Constructing a Simple Matrix
- •Entering Signed Numbers
- •Specialized Matrix Functions
- •Examples
- •Concatenating Matrices
- •Keeping Matrices Rectangular
- •Matrix Concatenation Functions
- •Examples
- •Generating a Numeric Sequence
- •The Colon Operator
- •Using the Colon Operator with a Step Value
- •Matrix Indexing
- •Accessing Single Elements
- •Linear Indexing
- •Functions That Control Indexing Style
- •Accessing Multiple Elements
- •Nonconsecutive Elements
- •The end Keyword
- •Specifying All Elements of a Row or Column
- •Using Logicals in Array Indexing
- •Logical Indexing – Example 1
- •Logical Indexing – Example 2
- •Logical Indexing with a Smaller Array
- •Single-Colon Indexing with Different Array Types
- •Indexing on Assignment
- •Arithmetic Operators
- •Arithmetic Operators and Arrays
- •Operator Precedence
- •Precedence of and and or Operators
- •Overriding Default Precedence
- •Relational Operators and Arrays
- •Relational Operators and Empty Arrays
- •Overview of the Logical Class
- •Logical Operators
- •Element-Wise Operators and Functions
- •Short-Circuit Operators
- •Precedence of and and or Operators
- •Symbol Reference
- •Asterisk — *
- •Filename Wildcard
- •Function Handle Constructor
- •Class Folder Designator
- •Line Continuation
- •Dynamic Structure Fields
- •Exclamation Point — !
- •Semicolon — ;
- •Array Row Separator
- •Output Suppression
- •Command or Statement Separator
- •Single Quotes — ' '
- •Square Brackets — [ ]
- •Fundamental matlab Classes
- •More About
- •Overview of Numeric Classes
- •Integers
- •Integer Classes
- •Creating Integer Data
- •Arithmetic Operations on Integer Classes
- •Largest and Smallest Values for Integer Classes
- •Integer Functions
- •Floating-Point Numbers
- •Double-Precision Floating Point
- •Single-Precision Floating Point
- •Creating Floating-Point Data
- •Creating Double-Precision Data
- •Creating Single-Precision Data
- •Arithmetic Operations on Floating-Point Numbers
- •Double-Precision Operations
- •Single-Precision Operations
- •Largest and Smallest Values for Floating-Point Classes
- •Largest and Smallest Double-Precision Values
- •Largest and Smallest Single-Precision Values
- •Accuracy of Floating-Point Data
- •Double-Precision Accuracy
- •Single-Precision Accuracy
- •Avoiding Common Problems with Floating-Point Arithmetic
- •Example 1 — Round-Off or What You Get Is Not What You Expect
- •Example 2 — Catastrophic Cancellation
- •Example 3 — Floating-Point Operations and Linear Algebra
- •Floating-Point Functions
- •Creating a Rectangular Character Array
- •Combining Strings Vertically
- •Combining Strings Horizontally
- •Identifying Characters in a String
- •Working with Space Characters
- •Expanding Character Arrays
- •String Comparisons
- •Comparing Strings for Equality
- •Comparing for Equality Using Operators
- •Categorizing Characters Within a String
- •Create a Structure Array
- •Access Data in a Structure Array
- •Concatenate Structures
- •Generate Field Names from Variables
- •Access Data in Nested Structures
- •Access Elements of a Nonscalar Struct Array
- •Create a Cell Array
- •Access Data in a Cell Array
- •Add Cells to a Cell Array
- •Delete Data from a Cell Array
- •Combine Cell Arrays
- •Pass Contents of Cell Arrays to Functions
- •Multilevel Indexing to Access Parts of Cells
- •Related Examples
- •What Is a Function Handle?
- •Creating a Function Handle
- •Maximum Length of a Function Name
- •The Role of Scope, Precedence, and Overloading When Creating a Function Handle
- •Obtaining Permissions from Class Methods
- •Example
- •Using Function Handles for Anonymous Functions
- •Arrays of Function Handles
- •Calling a Function Using Its Handle
- •Calling Syntax
- •Calling a Function with Multiple Outputs
- •Returning a Handle for Use Outside of a Function File
- •Example — Using Function Handles in Optimization
- •Preserving Data from the Workspace
- •Preserving Data with Anonymous Functions
- •Preserving Data with Nested Functions
- •Loading a Saved Handle to a Nested Function
- •Applications of Function Handles
- •Example of Passing a Function Handle
- •Pass a Function to Another Function
- •Example 1 — Run integral on Varying Functions
- •Example 2 — Run integral on Anonymous Functions
- •Example 3 — Compare integral Results on Different Functions
- •Capture Data Values For Later Use By a Function
- •Example 1 — Constructing a Function Handle that Preserves Its Variables
- •Example 2 — Varying Data Values Stored in a Function Handle
- •Example 3 — You Cannot Vary Data in a Handle to an Anonymous Function
- •Call Functions Outside of Their Normal Scope
- •Save the Handle in a mat-File for Use in a Later matlab Session
- •Parameterizing Functions
- •Overview
- •Parameterizing Using Nested Functions
- •Parameterizing Using Anonymous Functions
- •See Also
- •More About
- •Saving and Loading Function Handles
- •Invalid or Obsolete Function Handles
- •Advanced Operations on Function Handles
- •Examining a Function Handle
- •Converting to and from a String
- •Converting a String to a Function Handle
- •Converting a Function Handle to a String
- •Comparing Function Handles
- •Comparing Handles Constructed from a Named Function
- •Comparing Handles to Anonymous Functions
- •Comparing Handles to Nested Functions
- •Comparing Handles Saved to a mat-File
- •Overview of the Map Data Structure
- •Description of the Map Class
- •Properties of the Map Class
- •Methods of the Map Class
- •Creating a Map Object
- •Constructing an Empty Map Object
- •Constructing An Initialized Map Object
- •Combining Map Objects
- •Examining the Contents of the Map
- •Reading and Writing Using a Key Index
- •Reading From the Map
- •Adding Key/Value Pairs
- •Building a Map with Concatenation
- •Modifying Keys and Values in Map
- •Removing Keys and Values from the Map
- •Modifying Values
- •Modifying Keys
- •Modifying a Copy of the Map
- •Mapping to Different Value Types
- •Mapping to a Structure Array
- •Mapping to a Cell Array
R2013a>MATLAB>Language Fundamentals>Operators and Elementary Operations>Logical Operations
Overview of the Logical Class
The logical data type represents a logical true or false state using the numbers 1 and 0, respectively. Certain MATLAB® functions and operators return logical true or false to indicate whether a certain condition was found to be true or not. For example, the statement 50>40 returns a logical true value.
Logical data does not have to be scalar; MATLAB supports arrays of logical values as well. For example, the following statement returns a vector of logicals indicating false for the first two elements and true for the last three:
[30 40 50 60 70] > 40
ans =
0 0 1 1 1
This statement returns a 4-by-4 array of logical values:
x = magic(4) >= 9
x =
1 0 0 1
0 1 1 0
1 0 0 1
0 1 1 0
The MATLAB functions that have names beginning with is (e.g., ischar, issparse) also return a logical value or array:
a = [2.5 6.7 9.2 inf 4.8];
isfinite(a)
ans =
1 1 1 0 1
Logical arrays can also be sparse as long as they have no more than two dimensions:
x = sparse(magic(20) > 395)
x =
(1,1) 1
(1,4) 1
(1,5) 1
(20,18) 1
(20,19) 1
R2013a>MATLAB>Language Fundamentals>Operators and Elementary Operations>Logical Operations
Logical Operators
MATLAB offers three types of logical operators and functions:
Element-wise — operate on corresponding elements of logical arrays.
Bit-wise — operate on corresponding bits of integer values or arrays.
Short-circuit — operate on scalar, logical expressions.
The values returned by MATLAB logical operators and functions, with the exception of bit-wise functions, are of type logical and are suitable for use with logical indexing.
Element-Wise Operators and Functions
The following logical operators and functions perform element-wise logical operations on their inputs to produce a like-sized output array.
The examples shown in the following table use vector inputs A and B, where
A = [0 1 1 0 1];
B = [1 1 0 0 1];
Operator |
Description |
Example |
& |
Returns 1 for every element location that is true (nonzero) in both arrays, and 0 for all other elements. |
A & B = 01001 |
| |
Returns 1 for every element location that is true (nonzero) in either one or the other, or both arrays, and 0 for all other elements. |
A | B = 11101 |
~ |
Complements each element of the input array, A. |
~A = 10010 |
xor |
Returns 1 for every element location that is true (nonzero) in only one array, and 0 for all other elements. |
xor(A,B) = 10100 |
For operators and functions that take two array operands, (&, |, and xor), both arrays must have equal dimensions, with each dimension being the same size. The one exception to this is where one operand is a scalar and the other is not. In this case, MATLAB tests the scalar against every element of the other operand.
Note MATLAB converts any finite nonzero, numeric values used as inputs to logical expressions to logical 1, or true (например, 34&20 дает логическую 1). |
Operator Overloading. You can overload the &, |, and ~ operators to make their behavior dependent upon the class on which they are being used. Each of these operators has a representative function that is called whenever that operator is used. These are shown in the table below.
Logical Operation |
Equivalent Function |
A & B |
and(A, B) |
A | B |
or(A, B) |
~A |
not(A) |
Other Array Functions. Two other MATLAB functions that operate logically on arrays, but not in an element-wise fashion, are any and all. These functions show whether any or all elements of a vector, or a vector within a matrix or an array, are nonzero.
When used on a matrix, any and all operate on the columns of the matrix. When used on an N-dimensional array, they operate on the first nonsingleton dimension of the array. Or, you can specify an additional dimension input to operate on a specific dimension of the array.
The examples shown in the following table use array input A, where
A = [0 1 2;
0 -3 8;
0 5 0];
Function |
Description |
Example |
any(A) |
Returns 1 for a vector (столбец) where any element of the vector is true (nonzero), and 0 if no elements are true. |
any(A) ans = 0 1 1 |
all(A) |
Returns 1 for a vector (столбец) where all elements of the vector are true (nonzero), and 0 if all elements are not true. |
all(A) ans = 0 1 0 |
Note The all and any functions ignore any NaN values in the input arrays. |
Short-Circuiting in element-wise Operators. When used in the context of an if or while expression, and only in this context, the element-wise | and & operators use short-circuiting in evaluating their expressions. That is, A|B and A&B ignore the second operand, B, if the first operand, A, is sufficient to determine the result.
So, although the statement 1|[] evaluates to false, the same statement evaluates to true when used in either an if or while expression:
A = 1; B = [];
if(A|B) disp 'The statement is true', end;
The statement is true
while the reverse logical expression, which does not short-circuit, evaluates to false:
if(B|A) disp 'The statement is true', end;
Another example of short-circuiting with element-wise operators shows that a logical expression such as the following, which under most circumstances is invalid due to a size mismatch between A and B,
A = [1 1]; B = [2 0 1];
A|B % This generates an error.
works within the context of an if or while expression:
if (A|B) disp 'The statement is true', end;
The statement is true
Logical Expressions Using the find Function. The find (ind = find(X) locates all nonzero elements of array X, and returns the linear indices of those elements in vector ind. If X is a row vector, then ind is a row vector; otherwise, ind is a column vector. If X contains no nonzero elements or is an empty array, then ind is an empty array. [row,col,v] = find(X, ...) returns a column or row vector v of the nonzero entries in X, as well as row and column indices. If X isa logical expression, then v is a logical array.) function determines the indices of array elements that meet a given logical condition. The function is useful for creating masks and index matrices. In its most general form, find returns a single vector of indices. This vector can be used to index into arrays of any size or shape.
For example,
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
i = find(A > 8); % Ответ (для удобства преобразован): i = [1 3 6 8 10 12 13 15]'.
A(i) = 100
A =
100 2 3 100
5 100 100 8
100 7 6 100
4 100 100 1
Note An alternative to using find in this context is to index into the matrix using the logical expression itself. See the example below. |
The last two statements of the previous example can be replaced with this one statement:
A(A > 8) = 100;
You can also use find to obtain both the row and column indices of a rectangular matrix for the array values that meet the logical condition:
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
[row, col] = find(A > 12)
row =
1
4
4
1
col =
1
2
3
4
ДОПОЛНЕНИЕ. Команда
[row, col, v] = find(A > 12)
дает:
row =
1 % Соответствует A(1,1)>12.
4 % Соответствует A(4,2)>12.
4 % Соответствует A(4,3)>12.
1 % Соответствует A(1,4)>12.
col =
1 % Соответствует A(1,1)>12.
2 % Соответствует A(4,2)>12.
3 % Соответствует A(4,3)>12.
4 % Соответствует A(1,4)>12.
v =
1 % Соответствует A(1,1)>12.
1 % Соответствует A(4,2)>12.
1 % Соответствует A(4,3)>12.
1 % Соответствует A(1,4)>12.
Bit-Wise Functions
The following functions perform bit-wise logical operations on integer inputs. Inputs may be scalar or in arrays. If in arrays, these functions produce a like-sized output array.
The examples shown in the following table use scalar inputs A and B, where
A = 28; % binary 00011100
B = 21; % binary 00010101
Function |
Description |
Example |
bitand |
Returns the bit-wise AND of two integer arguments. |
bitand(A,B) = 20 (binary 00010100) |
bitor |
Returns the bit-wise OR of two integer arguments. |
bitor(A,B) = 29 (binary 00011101) |
bitcmp |
Returns the bit-wise complement, assuming the second argument specifies the integer data type of the first argument. By default, MATLAB treats double values as uint64 integers. |
bitcmp(A,'int8') = -29 (binary 11100011) |
bitxor |
Returns the bit-wise exclusive OR of two integer arguments. |
bitxor(A,B) = 9 (binary 00001001) |