
- •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
Using Logicals in Array Indexing
A logical array index designates the elements of an array A based on their position in the indexing array, B, not their value. In this masking type of operation, every true element in the indexing array is treated as a positional index into the array being accessed.
In the following example, B is a matrix of logical ones and zeros. The position of these elements in B determines which elements of A are designated by the expression A(B):
A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
B = logical([0 1 0; 1 0 1; 0 0 1]);
B =
0 1 0
1 0 1
0 0 1
A(B)
%{
Здесь A(B) - запрос на значения элементов A, соответствующих логическим единицам матрицы B; ответ - в виде массива-матрицы-вектора-столбца по принципу: A1=A(:); B1=B(:); A1(B1):
ans =
4
2
6
9
%}
The find function can be useful with logical arrays as it returns the linear indices of nonzero elements in B, and thus helps to interpret A(B):
find(B)
ans =
2
4
8
9
Теперь можно найти A(B) как
A(find(B))
ans =
4
2
6
9
Logical Indexing – Example 1
This example creates logical array B that satisfies the condition A > 0.5, and uses the positions of ones in B to index into A:
rng(0,'twister'); % Reset the random number generator (twister -тип генератора)
A = rand(5);
B = A > 0.5; % Поэлементный анализ; результат - массив B (логических единиц и нулей) по форме A.
A(B) = 0 % Здесь не запрос A(B), а присвоение элементам A, большим 0.5, значения 0; ответ - новый массив A:
A =
0 0.0975 0.1576 0.1419 0
0 0.2785 0 0.4218 0.0357
0.1270 0 0 0 0
0 0 0.4854 0 0
0 0 0 0 0
A simpler way to express this is
A(A > 0.5) = 0
Logical Indexing – Example 2
The next example highlights the location of the prime numbers in a magic square using logical indexing to set the nonprimes to 0:
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
B = isprime(A)
B =
0 1 1 1
1 1 0 0
0 1 0 0
0 0 0 0
A(~B) = 0; % Logical indexing (~B); ~ - оператор отрицания.
A
A =
0 2 3 13
5 11 0 0
0 7 0 0
0 0 0 0
find(B)
ans =
2
5
6
7
9
13
Logical Indexing with a Smaller Array
In most cases, the logical indexing array should have the same number of elements as the array being indexed into, but this is not a requirement. The indexing array may have smaller (but not larger) dimensions:
A = [1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
B = logical([0 1 0; 1 0 1])
B =
0 1 0
1 0 1
isequal(numel(A), numel(B)) % Операция сравнения; numel - число элементов массива.
ans =
0
A(B) % Ответ дается для начальных элементов A числом numel(B):
ans =
4
7
8
MATLAB treats the missing elements of the indexing array as if they were present and set to zero, as in array C below:
% Add zeros to indexing array C to give it the same number of
% elements as A.
C = logical([B(:);0;0;0]); % Указание на класс logical необходимо для нулей.
isequal(numel(A), numel(C))
ans =
1
A(C) % Здесь ответ дается для всех элементов A:
ans =
4
7
8