Matlab programming and application series-Chapter 1 matrix operations (3)

Source: Internet
Author: User

This series of tutorials comes from the publication of the "Matlab-based programming basics and typical application books". If copyright concerns are involved, please contact [email protected]. Press: People's post and telecommunications press. The number of pages is 525.

This series of tutorials is currently based on MATLAB r2006a and may differ from functions in more advanced versions. If you have any questions in this tutorial, contact [email protected].

### 3.1.8 comparison of matrix and Array Operations
When performing matrix and array operations, we need to recognize the differences between similar operations on Matrices and arrays. the similarities and differences between the two types of operation commands and their actual meanings are shown in Table 3.3. Where S is a scalar, And A and B are matrices.
Table 3.3 comparison of matrix and Array Operations
Array Operations Matrix Operations
Command Description Command Description
A+B Add corresponding elements A+B Same as Array Operations
A-B Subtraction of corresponding elements A-B Same as Array Operations
S.*B Product of scalar S and Element B respectively S*B Same as Array Operations
A.*B Multiply elements of the array A*B Product of the same matrix of the inner dimension
S./B S is divided by the Left Division of B's elements. S\B The B matrix is separated by S on the left.
A./B Elements of a are divided by corresponding elements of B. A/B Right Division B of matrix A, that is, the inverse matrix of A is multiplied by B.
B.\A The result must be the same as that of the upstream B\A A left Division B (generally different from the uplink)
A.^S Each element of A is multiplied by S. A^S When matrix A is a square matrix, it is multiplied by seconds.
A.^S When S is a decimal number, calculate the non-integer power of each element of a to obtain the matrix. A^S When S is a decimal number, the non-integer multiplication of square matrix
S.^B Calculate the power using the element of B as the exponent. S^B When B is a square matrix, the matrix multiplication of scalar s
A.’ Non-centers, equivalent to conj (') A’ Transpose
exp(A) Calculate the power based on natural number E and the elements of a respectively. expm(A) Matrix exponent function of
log(A) Evaluate the logarithm of each element of logm(A) Matrix logarithm function of
sqrt(A) Returns the square root of each element of.sqrtm(A) Matrix square root function of
f(A) Evaluate the function values of each element of funm(A,’FUN’) Matrix Function operations

Note: (1) funm (A, 'fun ') requires a to be a phalanx, and "fun" is the name of the matrix operation function.
(2) When performing an operation between an array and an array, the array involved in the operation must be the same dimension, and the resulting array is also the same as the original array.

### 3.1.9 relational and logical operations
When using the MATLAB language for program design, the control of the program process is often involved. At this time, some problems need to be judged, therefore, MATLAB provides relational operations, logical computing, and related computing functions.

(1) relational operation
Relational operators include: <(less than link), <= (less than or equal to link),> (greater than link),> = (greater than or equal to link), and = (equal to link) ,~ = (Not equal to the link ).

The relational calculation rules are as follows:

  • If both variables are scalar, the result is true (1) or false (0 );
  • If both variables are arrays, they must be of the same size and the result is an array of the same size. The elements of the array are 0 or 1;
  • If an array and a scalar are used, each element of the array is compared with a scalar, and the result is an array of the same size as the array. The element of the array is 0 or 1.
  • <, <= And>,> =, compares only the real part of the variable to be compared, and ~ =, The real and virtual parts are compared at the same time.

[Example 3.20] relational operation
In the command window, enter the following content to generate the matrices A and B:
&gt;&gt; A=[1 2 3 4 5;5 3 4 8 5;6 8 7 6 9;2 6 8 2 1;2 7 9 3 9];
&gt;&gt; B=[5 3 4 8 5;6 8 7 6 9;2 6 8 2 1;2 7 9 3 9;4 5 6 7 8];
① Determine whether the elements of matrix A and B are the same and return the result to matrix C1. In the command window, enter:
& Gt; C1 = A = B % A. If the elements of matrix B are equal, 1 is returned. Otherwise, 0 is returned.
② Determine whether the element of matrix A is greater than or equal to the element corresponding to matrix B and return the result to matrix C2. In the command window, enter:
& Gt; C2 = A & gt; = B % A, B matrix corresponding to element a greater than or equal to B returns 1; otherwise, return 0
③ Determine whether the elements of matrix A and B are different and return the results to matrix C3. In the command window, enter:
& Gt; C3 = ~ = Returns 0 if the elements of matrix B % A and matrix B are equal. Otherwise, 1 is returned.

(2) logical operations
Logical operators include: & (and), | (OR ),~ (Not) and XOR (exclusive or ). In logical operations, non-0 elements indicate true (1), 0 element table false (0), and the logical operation result is 0 or 1. The logical operation rules are shown in table 3.5.

Table 3.5 logical operations

A B A & B A or B ~ A XOR (A, B)
0 0 0 0 1 0
0 1 0 1 1 1
1 0 0 1 0 1
1 1 1 1 0 0

Logical operations must meet the following operational rules:

  • If both variables are scalar, the result is a scalar of 0 and 1;
  • If both variables are arrays, they must be of the same size and the results are arrays of the same size;
  • If an array and a scalar are used for logical operations, each element of the array is compared with the scalar, and the result is an array of the same size as the array.

In addition to the preceding logical operators, Matlab also provides & (prerequisite and) logical operators and | (prerequisite or) logical operators.

& (Prerequisite and) when the left side of the operator is 1 (true), the operation on the right side of the operator is continued and executed with the logical operation; otherwise, the operation on the right of the operator is no longer executed, and the result of the operation is 0 (false ).

| (Prerequisite or) when the left side of the operator is 1 (true), you do not need to continue the operation on the right side of the operator, the logical operation result is 1 (true) immediately. Otherwise, the operation on the right of the symbol is executed and the logical operation is performed.

(Example 3.21) logical operation
In the command window, enter the following content to generate the matrices A and B:
&gt;&gt; A=[1 2 3 4 5;5 3 4 8 5;6 8 7 6 9;2 6 8 2 1;2 7 9 3 9];
&gt;&gt; B=[5 3 4 8 5;6 8 7 6 9;2 6 8 2 1;2 7 9 3 9;4 5 6 7 8];
① If the logic and value between A and B are calculated, and the result is returned to the matrix C1. In the command window, enter:
& Gt; C1 = A & B % calculate the logic and value between A and B
② If the logic or value between A and B is calculated, the result is returned to matrix C2. In the command window, enter:
& Gt; C2 = A | B % calculate the logical or value between A and B
③ If the logic of A is not calculated, the result is returned to matrix C3. In the command window, enter:
& Gt; C3 = ~ A % calculates the logical non-
④ If the logic difference or between A and B is calculated, the result is returned to the C4 matrix. In the command window, enter:
& Gt; C4 = XOR (a, B) % calculate the logical XOR between A and B

[Example 3.22] prerequisite logical operations
In the command window, enter the following variables A, B, and C:
&gt;&gt; a=0;
&gt;&gt; b=5;
&gt;&gt; c=10;
① Perform the prerequisites and operations, and enter the following content in the Command window:
&gt;&gt; (a~=0)&&(b&lt;c)
The calculation result is as follows:
ans =<br/>0
② Perform the prerequisites and operations, and enter the following content in the Command window:
&gt;&gt; (a~=0)||(b&lt;c)
The calculation result is as follows:
ans =<br/>1

(3) logical function operations

Some functions provided in MATLAB return true (1) and false (0). Such functions include relational logic functions, working state judgment functions, special data judgment functions, and data type functions. Table 3.6 shows the relational logical functions and their functional descriptions.

Table 3.6 relational Logical Functions

Function Name Function Function Name Function
all(A) Determines whether all column vector elements of a are not 0, and all columns other than 0 are 1. isnan(A) Determine whether the element value of A is Nan. If yes, it is 1.
any(A) Determines whether the column vector element of A contains a non-0 element. If yes, it is 1. isnumeric(A) Determines whether all elements of array a are numeric arrays.
isequal(A,B) Determine whether all elements of A and B are equal to 1. isreal(A) Determines whether all elements of array a are real numbers. If yes, it is 1.
isempty(A) Determines whether a is an empty matrix. If it is null, it is 1. Otherwise, it is 0. isprime(A) Determines whether each element value of A is a prime number. If yes, it is 1.
isfinite(A) Determine whether the value of each element of a is limited. If yes, it is 1. isspace(A) Determines whether each element value of A is a space. If yes, it is 1.
isinf(A) Determines whether the value of each element of a is infinite. If yes, it is 1. find(A) Returns the subscript and value of Non-0 elements in array.

(4) operator priority

MATLAB provides a variety of operator numbers. The execution sequence of operators is determined based on their priorities. The priorities of operators are listed below, which are descending from top to bottom. The priorities of each line are the same:

A) parentheses ()
B) '(matrix transpose), ^ (matrix power),.' (array transpose),. ^ (array power)
C )~ (Non-logical), positive algebra +, negative algebra-
D)(Multiplication),/(left Division), (right division), and.(Point multiplication),./(point left Division),. (point right Division)
E) +,-(addition and subtraction)
F) Colon:
G) <, <=,>,> = ,~ =
H) & (logic and)
I) | (logical or)
J) & (prerequisite and)
K) | (prerequisite or)

DT data
Contact information: [email protected]

Matlab programming and application series-Chapter 1 matrix operations (3)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.