MATLAB's commonly used internal functions, operations, string and structure data and unit data

1. Commonly used internal functions

  • Internal functions are a group of programs compiled and provided to users by MATLAB system according to the needs of general users, also called system functions or library functions.

1. Common mathematical functions

  • MATLAB provides many mathematical functions. The independent variable of the function is specified as a matrix variable, and the operation method is to apply the function to the elements of the matrix item by item. Therefore, the result of the operation is a matrix with the same dimension and size as the independent variable, that is, the result matrix has the same type as the argument matrix. For example, we can change the number of each element in the A matrix.
>> A=[4,2;3,6]

A =

     4     2
     3     6

>> B=sqrt(A)

B =

    2.0000    1.4142
    1.7321    2.4495

  • Some common mathematical functions in MATLAB are shown in the table below.
Function name Function Function name Function
sin/are Sine function, the input value is radians/degrees abs absolute value function
cost Cosine function, the input value is radian/degree rem Surplus
tan/tand Tangent function, the input value is radians/degrees mod modulus
salty/salty Inverse sine function, the return value is radian/degree fix Round towards zero
acos/acosd Inverse cosine function, the return value is radian/degree floor largest integer not greater than the argument
atan/atand Arctangent function, the return value is radian/degree ceil the smallest integer not less than the argument
birth/asinh Hyperbolic Sine/Inverse Hyperbolic Sine round round to nearest integer
cosh/acosh Hyperbolic Cosine Function / Inverse Hyperbolic Cosine Function sign symbolic function
fishy/atanh Hyperbolic Tangent/Inverse Hyperbolic Tangent gcd greatest common divisor
sqrt square root function lcm least common multiple
log natural log function factorial factorial
log10 Common logarithmic function they receive Determine whether it is a prime number
log2 base 2 logarithmic function primes Generate a list of prime numbers
exp natural exponential function perms generate all permutations
pow2 power of 2 beach perm Generate any permutation
  • Function instructions are as follows.
  • (1) Trigonometric functions include functions with radian as the unit and functions with angle as the unit. For functions with angle as the unit, d is added after the function name to show the difference.
  • (2) The abs function can calculate the absolute value of a real number, the modulus of a complex number, and the ASCII code value of a string. For example, the values ​​of abs(-4), abs(3+4i), abs('a') are 4, 5, 97 respectively.
  • (3) The remainder operation and the modulo operation have the same place but are not exactly the same. The main difference lies in the operation of division of negative integers.
  • For integers a and b, the method of remainder operation or modulus operation is to find the integer quotient c=a/b first, and then find the remainder or modulo r=ac*b. The remainder operation rounds towards 0 when taking the value of c ( fixfunction), while the modulo operation rounds towards negative infinity when calculating the value of c ( floorfunction).
  • remThe difference with modthe function is that when b≠0, rem(a,b)=ab.*fix(a./b), and mod(a,b)=ab.*floor(a/b); when b =0, rem(a,0)=NaN, and mod(a,0)=a.
  • Obviously, if a and b have the same sign, then rem(a,b)=mod(a,b). If the signs of a and b are opposite, then mod(a,b)=rem(a,b)+b.
  • rem(a,b) has the same sign as a, and mod(a,b) has the same sign as b. For example, rem(7,4)=mod(7,4)=3; rem(-7,-4)= -mod(-7,-4)=-3; rem(7,-4)=3, And mod(7,-4)=-1; rem(-7,4)=-3, and mod(-7,4)=1.
  • (4) Functions for rounding include fix, floor, ceil, and round. Pay attention to their differences. roundThe role of the function is to round up. Let a be the positive integer closest to x (|x|≥a), then the difference of the remaining 3 functions can be shown in the figure below.

insert image description here

  • Let x=2.45, then the results of fix(x), floor(x), ceil(x), round(x) are 2, 2, 3, 2 respectively.
  • Let x= -2.65, then the results of fix(x), floor(x), ceil(x), round(x) are -2, -3, -2, -3 respectively.
  • (5) About symbolic functions. When x<0, sign(x)=-1; when x=0, sign(x)=0; when x>0, sign(x)=1.

2. Transcendental functions of matrices

  • MATLAB provides some transcendental functions that act directly on matrices. These function names are all suffixed with m after the above internal function names, and the input parameter A must be a square matrix.

2.1 Matrix square root

  • sqrtm(A)Compute the square root A \sqrt{A} of matrix AA . For example:
>> A=[4,2;3,6];
>> B=sqrtm(A)

B =

    1.9171    0.4652
    0.6978    2.3823

>> B*B

ans =

    4.0000    2.0000
    3.0000    6.0000

  • If A is a real symmetric positive definite matrix, its square root must be calculated. But some matrices, such as A=[0,1;0,0] will not get the square root. If matrix A has negative eigenvalues, sqrtm(A) will result in a complex matrix, for example:
>> A=[4,9;16,25];
>> eig(A)  %矩阵 A 的特征值

ans =

   -1.4452
   30.4452

>> B=sqrtm(A)

B =

   0.9421 + 0.9969i   1.5572 - 0.3393i
   2.7683 - 0.6032i   4.5756 + 0.2053i

2.2 Matrix logarithms

  • logm(A)Computes the natural logarithm of matrix A. The relationship between the condition of the input parameters of this function and the output result is sqrtm(A)exactly , for example:

>> A=[4,9;1,5];
>> L=logm(A)

L =

    1.0639    2.4308
    0.2701    1.3340

2.3 Matrix index

  • expm(A)The function is to find the matrix index e A e^{A}eA. _ For example, for the natural logarithm L of A calculated above, find its matrix indexB = e LB=e^{L}B=eL
>> B=expm(L)

B =

    4.0000    9.0000
    1.0000    5.0000
 
  • From this result, we know that B and A are exactly the same, that is, expm(A)the function and logm(A)the function are reciprocal.

2.4 Ordinary matrix functions

  • funm(A,@fun)Computes the matrix function value of the function defined by fun for the matrix A. For example, when fun takes exp, funm(A,@fun)the exponent of matrix A can be calculated, which is the same as the calculation result expm(A)of .
>> A=[2,-1;1,0];
>> funm(A,@exp)

ans =

    5.4366   -2.7183
    2.7183         0

>> expm(A)

ans =

    5.4366   -2.7183
    2.7183         0

  • funmFunctions can be used for functions such as exp, log, sin, cos, sinh, and cosh, but only sqrtmfunctions .

2. MATLAB operation

  • MATLAB's operations are all for matrices, including operations in the sense of matrices and operations on matrix elements. In terms of the nature of operations, there are arithmetic operations, relational operations, and logical operations.

1. Arithmetic operations

1.1 Basic Arithmetic Operations

  • The basic arithmetic operations of MATLAB include + (addition), - (subtraction), * (multiplication), / (right division), \ (left division), ^ (power).
  • The operation rules of these arithmetic operations are not difficult to understand, but it must be noted that the operation is carried out in the sense of matrix, and the arithmetic operation of single data is only a special case.
  • (1) Matrix addition and subtraction operations. Assuming that there are two matrices A and B, the addition and subtraction of matrices can be realized by A+B and AB.
  • The operation rule is that if A and B are of the same type, matrix addition and subtraction operations can be performed, and the corresponding elements of A and B are added and subtracted. If A and B are of different types, MATLAB will give an error message stating that the dimensions or sizes of the two matrices do not match.
  • A scalar can also perform addition and subtraction operations with a matrix, and at this time, perform addition and subtraction operations between the scalar and each element of the matrix.
>> x=[2,-1,0;3,2,-4];
>> y=x-1

y =

     1    -2    -1
     2     1    -5

  • (2) Matrix multiplication. Suppose there are two matrices A and B, if A is an m×n matrix and B is an n×p matrix, then C=A×B is an m×p matrix, and each element is C ij = ∑ k = 1 naik . bkj C_{ij}=\sum_{k=1}^{n}a_{ik}.b_{kj}Cij=k=1nai.bkj
  • Among them, i is taken from 1 to m, and j is taken from 1 to p.
>> A=[1,2,3;4,5,6];
>> B=[1,2;3,0;7,4];
>> C=A*B

C =

    28    14
    61    32

  • The multiplication of matrices A and B requires the number of columns of A to be equal to the number of rows of B. At this time, the matrices A and B are said to be multiplicative, or the two matrices A and B are said to be compatible in dimension and size. If the two dimensions or sizes are not compatible, an error message will be given stating that the two matrices are not multiplicative.
>> A=[10,20,30;40,50,60];
>> B=[1,3;4,8];
>> C=A*B
错误使用  * 
用于矩阵乘法的维度不正确。请检查并确保第一个矩阵中的列数与第二个矩阵中的行数匹配。要单独对矩阵的每个元素进行运算,请使用 TIMES
(.*)执行按元素相乘。

相关文档
 
  • It means that the dimensions or sizes of the two matrices A and B are incompatible, and matrix multiplication cannot be performed.
  • In MATLAB, matrix and scalar multiplication operations can also be performed, and the scalar can be either a multiplier or a multiplicand. Matrix and scalar multiplication is the multiplication of each element in the matrix by this scalar.
  • (3) Matrix division. In MATLAB, there are two matrix division operations: \ and /, which represent left division and right division respectively. If the A matrix is ​​a non-singular square matrix, the A\B and B/A operations can be implemented.
  • A\B is equivalent to the inverse left multiplication of the B matrix of A, that is, inv(A) * B; and B/A is equivalent to the inverse right multiplication of the A matrix by the B matrix, that is, B * inv(A).
  • For operations involving scalar quantities, the two division operations have the same result, eg 3/4 and 4\3 have the same value, equal to 0.75. For another example, if a=[10.5,25], then a/5= -5\a=[2.1000 5.0000].
  • For a matrix, left division and right division represent two different relationships between the divisor matrix and the dividend matrix. For matrix operations, generally A\B≠B/A.
>> A=[1,2,3;4,2,6;7,4,9];
>> B=[4,3,2;7,5,1;12,7,92];
>> C1=A/B

C1 =

    7.9623   -4.2453   -0.0943
   -4.5472    2.9434    0.1321
   -5.1321    3.6415    0.1698

>> C2=A\B

C2 =

    0.5000   -0.5000   44.5000
    1.0000    0.0000   46.0000
    0.5000    1.1667  -44.8333

  • Obviously, C1≠C2.
  • (4) The power of the matrix. The power operation of a matrix can be expressed as A^x, requiring A to be a square matrix and x to be a scalar.
>> A=[1,2,3;4,5,6;7,8,0];
>> A^2

ans =

    30    36    15
    66    81    42
    39    54    69

  • Obviously, A^2 is A*A.
  • The square root of a matrix is ​​quite difficult, but with a computer, this kind of operation is no longer so troublesome. We can use a computer to find the square root of a matrix conveniently.
>> A=[1,2,3;4,5,6;7,8,0];
>> A^0.1

ans =

   0.9750 + 0.2452i   0.1254 - 0.0493i   0.0059 - 0.0604i
   0.2227 - 0.0965i   1.1276 + 0.1539i   0.0678 - 0.1249i
   0.0324 - 0.1423i   0.0811 - 0.1659i   1.1786 + 0.2500i

1.2 Point operation

  • In MATLAB, there is a special operation, because its operator is to add a dot in front of the relevant arithmetic operator, so it is called dot operation.
  • The dot operators are .*, ./, .\, and .^. The point operation of two matrices refers to the correlation operation of their corresponding elements, which requires the two matrices to be of the same type.
>> A=[1,2,3;4,5,6;7,8,9];
>> B=[-1,0,1;1,-1,0;0,1,1];
>> C=A.*B

C =

    -1     0     3
     4    -5     0
     0     8     9

  • A .* B represents the multiplication between the corresponding elements of A and B, which are completely different concepts from A * B.
  • At the same time, if the two matrices A and B are of the same type, then A./B means the corresponding elements of matrix A divided by matrix B. B.\A is equivalent to A./B.
>> A=[1,2,3;4,5,6];
>> B=[-2,1,3;-1,1,4];
>> C1=A./B

C1 =

   -0.5000    2.0000    1.0000
   -4.0000    5.0000    1.5000
   
>> C2=B.\A

C2 =

   -0.5000    2.0000    1.0000
   -4.0000    5.0000    1.5000

  • Clearly, A./B and B.\A are equal in value. This is different from the left division and right division of the matrix introduced earlier.
  • If the two matrices are of the same type, A.^B means that the corresponding elements of the two matrices are multiplied. Among them, the exponent can be a scalar, and the base can also be a scalar.
>> A=[1,2,3];
>> B=[4,5,6];
>> C=A.^B

C =

     1    32   729

>> C1=A.^2

C1 =

     1     4     9

>> C2=2.^[A,B]

C2 =

     2     4     8    16    32    64

  • It should be noted here that the dot operation is a special operator in MATLAB. For example, when x=0.1, 0.4, 0.7, 1, find y = sin ⁡ x cos ⁡ xy=\sin x\cos xy=sinxcosThe value of x , the command should be written as
>> x=0.1:0.3:1;
>> y=sin(x).*cos(x)

y =

    0.0993    0.3587    0.4927    0.4546

  • Among them, the expression of y must be a dot multiplication operation. If x has only one value, multiplication is sufficient.

2. Relational operations

  • MATLAB provides 6 relational operators: < (less than), <= (less than or equal to), > (greater than), >= (greater than or equal to), == (equal to), ~= (not equal to). Their meaning is not difficult to understand, but it should be noted that their writing method is not the same as the inequality symbols in mathematics.
  • MATLAB also provides functions corresponding to the 6 relational operators It, le, gt, ge, eq, ne.
  • The operation rules of relational operators are as follows.
  • (1) When the two comparison quantities are scalar quantities, directly compare the magnitudes of the two numbers. If the relationship is established, the result of the relational expression is 1, otherwise it is 0.
>> 12>3

ans =

  logical

   1

>> eq(2,3)

ans =

  logical

   0

  • (2) When the quantities involved in the comparison are two matrices of the same type, the comparison is performed on the elements at the same position of the two matrices one by one according to the scalar relationship operation rules, and the element comparison result is given. The result of the final relational operation is a matrix with the same type as the original matrix, whose elements are composed of 0 or 1.
  • (3) When one of the comparisons is a scalar and the other is a matrix, compare the scalar and each element of the matrix one by one according to the scalar relationship operation rules, and give the element comparison result. The result of the final relational operation is a matrix with the same type as the original matrix, whose elements are composed of 0 or 1.
  • For example, we generate a 5th-order random square matrix A whose elements are random integers in the interval [10,90], and then judge whether the elements of A are divisible by 3.
>> A=fix((90-10+1)*rand(5)+10)

A =

    75    17    22    21    63
    83    32    88    44    12
    20    54    87    84    78
    83    87    49    74    85
    61    88    74    87    64

>> P=rem(A,3)==0  %等价于 P=eq(rem(A,3),0)

P =

  5×5 logical 数组

   1   0   0   1   1
   0   0   0   0   1
   0   1   1   1   1
   0   1   0   0   0
   0   0   0   1   0
   
  • where rem(A,3) is the remainder matrix of dividing each element of matrix A by 3. When judging whether the remainder matrix is ​​0 or not, 0 is extended to a zero matrix of the same type as A, and P is the comparison result matrix.
  • In addition, MATLAB also provides some relational operation functions, as shown in the following table.
Function name meaning
all The result is 1 if all elements of the vector are nonzero, otherwise 0
any The result is 1 if any element in the vector is non-zero, otherwise 0
exist Checks if the variable exists in the workspace, returns 1 if it does, 0 otherwise
find Find Locations of Nonzero Elements in Vector or Matrix
isempty If the checked variable is an empty matrix, the result is 1, otherwise it is 0
for you If the element is ±tinf, the element at the corresponding position of the result matrix is ​​1, otherwise it is 0
isan If the element is nan, the element in the corresponding position of the result matrix is ​​1, otherwise it is 0
isfinite If the size of the element value is limited, the element at the corresponding position of the result matrix is ​​1, otherwise it is 0
syntax If the checked variable is an integer, it takes 1, otherwise it takes 0
isnumeric If the checked variable is a numeric value, it takes 1, otherwise it takes 0
Israel If the checked variable is a real number, it takes 1, otherwise it takes 0
isfloat If the checked variable is a floating point type, take 1, otherwise take 0\
  • For example, we build matrix A, then find out the position of the element greater than 4, and output the element at the corresponding position.
>> A=[4,-65,-54,0,6;56,0,67,-45,0]

A =

     4   -65   -54     0     6
    56     0    67   -45     0

>> k=find(A>4)

k =

     2
     6
     9

>> A(k)

ans =

    56
    67
     6

  • It should be noted here that findthe position of the matrix element obtained by the function is represented by the serial number of the corresponding element.

3. Logical operations

  • MATLAB provides 3 logical operators: & (and), | (or), and ~ (not). In addition, it also provides 4 logical operation functions: and(a,b), or(a,b), not(a)and xor(a,b), which respectively represent AND, OR, NOT, and XOR operations.
  • In logical operations, a non-zero element is true and is represented by 1; a zero element is false and is represented by 0.
  • Assuming that two scalars a and b participate in the logical operation, then the meaning of the logical operation is as follows.
  • (1) a&b or function and(a, b): when a and b are both non-zero, the result is 1; as long as one of a and b is zero, the result is 0.
  • (2) a|b or function or(a,b): when only one of a and b is non-zero, the result is 1; when a and b are both zero, the result is 0.
  • (3) ~a and function not(a): when a is zero, the result is 1; when a is non-zero, the result is 0.
  • (4) Function xor(a,b): When the values ​​of a and b are different, the result is 1; when the values ​​of a and b are the same, the result is 0.
  • Logical operations also have the following arithmetic rules.
  • (1) If two matrices of the same type are involved in the logical operation, the operation will be performed on the elements at the same position of the matrix one by one according to the scalar rules. The final operation result is a matrix with the same type as the original matrix, whose elements are composed of 1 or 0.
  • (2) If one of the logical operations is a scalar and the other is a matrix, then the operation will be performed one by one between the scalar and each element in the matrix according to the scalar rules. The final operation result is a matrix with the same type as the matrix, whose elements are composed of 1 or 0.
  • (3) Logical NOT is a unary operator and also obeys the rules of matrix operation.
  • Among arithmetic operations, relational operations, and logical operations, arithmetic operations have the highest priority and logical operations have the lowest priority.

3. String

  • In MATLAB, there are two basic data types, one is numerical data, and the other is character data or string data.
  • Numerical data is easier to understand. It refers to data that can participate in numerical operations, and is divided into integer, floating-point, and complex numbers.
  • Character data is less emphasized in daily applications, but it exists in large quantities, such as counting the number of occurrences of different English letters in an English article, sorting by name, and so on. String data consists of a number of characters, which can be any characters allowed on the computer system.

1. Representation of strings

  • In MATLAB, a string is a sequence of characters enclosed in single quotes.
>> x='yan zi 22'

x =

    'yan zi 22'

  • If a character in the string contains a single quotation mark, the single quotation mark character must be represented by two single quotation marks.
>> 'I ''m a student.'

ans =

    'I 'm a student.'

  • MATLAB treats a string as a row vector, each element corresponds to a character, and its reference method is the same as that of a numeric vector.
>> A='ABCDEF';
>> A(1:3)

ans =

    'ABC'

  • It is also possible to create multi-line strings to form a string matrix.
>> A=['abcdef';'123456'];
>> A(2,3)

ans =

    '3'

  • It should be noted that the number of characters in each line is required to be equal. For this reason, spaces sometimes have to be used to adjust the lengths of the lines so that they are equal to each other.
  • For example, we create a vector of strings, and then process the vector as follows.
  • (1) Take the substring composed of the 1st~5th characters.
  • (2) Rearrange the strings upside down.
  • (3) Change the lowercase letters in the string to the corresponding uppercase letters, and keep the rest of the characters unchanged.
  • (4) Count the number of lowercase letters in the string.
>> A='ABc123d4e56Fg9';
>> A1=A(1:5)

A1 =

    'ABc12'

>> A2=A(end:-1:1)

A2 =

    '9gF65e4d321cBA'

>> k=find(A>='a'&A<='z');
>> A(k)=A(k)-('a'-'A')

A =

    'ABC123D4E56FG9'

>> length(k)

ans =

     4

2. String operations

2.1 String Execution

  • An important function related to strings is that evalits function is to execute the content of the string as a corresponding MATLAB command, and its calling format is as follows:
>> eval(s)

  • where s is a string.
>> t=pi;
>> m='[t,sin(t),cos(t)]';
>> y=eval(m)

y =

    3.1416    0.0000   -1.0000

2.2 Conversion between strings and numbers

  • The string is stored in the form of ASCII code, absand doublethe function can be used to obtain the ASCII code value matrix corresponding to the string matrix. charThe function can convert an ASCII code matrix into a string matrix.
>> s1='MATLAB';
>> a=abs(s1)

a =

    77    65    84    76    65    66

>> b=double(s1)

b =

    77    65    84    76    65    66

>> char(a+32)

ans =

    'matlab'

  • MATLAB also has many functions for converting between string and numeric data.
  • setstrThe function converts an ASCII code value into the corresponding character.
  • str2numfunction or str2doublefunction converts a string of numbers to a number.
  • num2strFunction converts numeric values ​​to strings.
  • int2strFunction converts an integer to a string.

2.3 String concatenation

  • In MATLAB, there are two common ways to concatenate two strings: one is with a string vector, and the other is with strcatthe function .
  • String vectors can be used to concatenate several strings, that is, to enclose several strings in square brackets to obtain a longer string.
>> f=70;
>> c=(f-32)/1.8;
>> ['Room temperature is ',num2str(c),' degree C.']

ans =

    'Room temperature is 21.1111 degree C.'

  • strcatThe function can concatenate several strings.
>> strcat('yan','zi','22')

ans =

    'yanzi22'

2.4 Comparison of strings

  • There are two ways to compare strings, using relational operators or string comparison functions.
  • When two strings have the same length, you can use relational operators to compare the strings. The comparison rule is to compare characters one by one according to the size of the ASCII value. The result of the comparison is a numeric vector whose elements are the corresponding characters The result of the comparison.
>> 'www0'>='W132'

ans =

  1×4 logical 数组

   1   1   1   0

  • The string comparison function is used to judge whether the strings are equal. There are 4 comparison methods, and the functions are as follows.
  • (1) strcmp(s1,s2): It is used to compare whether the strings s1 and s2 are equal, if they are equal, return 1, otherwise return 0.
  • (2) strncmp(s1,s2,n): It is used to compare whether the first n characters are equal, if they are equal, return 1, otherwise return 0.
  • (3) strcmpi(s1,s2): Under the premise of ignoring the case of letters, compare whether the strings s1 and s2 are equal, if they are equal, return 1, otherwise return 0.
  • (4) strnempi(s1,s2,n): Under the premise of ignoring the case of the string, compare whether the first n characters are equal, if they are equal, return 1, otherwise return 0.
>> strcmp('www0','W123')

ans =

  logical

   0
 
>> strncmpi('ww0','W12',1)

ans =

  logical

   1
 

2.5 Find and replace strings

  • MATLAB provides many functions for finding and replacing characters in strings. Commonly used are the following two.
  • (1) findstr(s1,s2): Return the start position of the short string in the long string (the order of s1 and s2 is not required).
>> p1=findstr('this is a test','is')

p1 =

     3     6

>>  p2=findstr('is','this is a test')

p2 =

     3     6

  • The short string is appears twice in the long string this is a test, starting at positions 3 and 6, respectively.
  • (2) strep(1,s2,s3): Replace all substrings s2 in string s1 with string s3.
>> p3=strrep('this is a test','test','success')

p3 =

    'this is a success'

4. Structural data and unit data

  • MATLAB adds two new data types: the structure data type and the element data type.
  • Both of these two data types integrate different related data into a single variable, which makes the processing
    and reference of a large amount of related data simple and convenient.

1. Structural data

  • Personal feeling can refer to the structure in C++.
  • Structural data types form a group of logically related data of different types into an organic whole for easy management and reference. For example, if you want to store the basic information of students, you can use the structure data type.

1.1 Establishment and reference of structural data

  • The elements of the structure matrix can be of different data types, and it can manage a set of data with different attributes under a unified variable name. To create a structure matrix, you can use the method of assigning values ​​to structure members, and its format is as follows:
    结构矩阵名.成员名=表达式
  • Here, the expression should be understood as a matrix expression. For example, we want to create a structure matrix a with 3 elements, the command is as follows:
>> a(1).x1=10;a(1).x2='liu';a(1).x3=[11,21;34,78];
>> a(2).x1=12;a(2).x2='wang';a(2).x3=[34,191;27,578];
>> a(3).x1=14;a(3).x2='cai';a(3).x3=[13,180;57,231];
  • It should be noted here that members of structure matrix elements can also be structure data.
>> a(2).x1.x11=90;a(2).x1.x12=12;a(2).x1.x13=30;
  • The structure matrix a established above contains 3 elements, each element contains 3 members, and the member a(2).x1 is the structure data containing 3 members.
  • A reference to a structure data can refer to its members, or to an element of a structure matrix or a structure variable.
 >> a(2).x3

ans =

    34   191
    27   578

>> a(2)

ans = 

  包含以下字段的 struct:

    x1: 12
    x2: 'wang'
    x3: [2×2 double]

>> a

a = 

  包含以下字段的 1×3 struct 数组:

    x1
    x2
    x3

  • When a member of a structure matrix element is referenced, its value is displayed. When referencing a structure matrix element, the member name and its value are displayed, but when the member is a matrix, its specific content is not displayed, only the size parameter of the member matrix is ​​displayed. When referencing a structure matrix, only the size parameter and member names of the structure matrix are displayed.

1.2 Modification of structure members

  • Members of the structure can be added or removed as needed. For example, if we want to add a member x4 to the structure matrix a, member x4 can be added to any element in a, the command is as follows:
>> a(1).x4='410075';
  • But other members are empty matrix, you can use the assignment statement to assign a certain value to it.
  • To delete a member of a structure, this can be done using rmfieldthe function . For example, to delete member x4, the command is as follows:
>> a=rmfield(a,'x4');

2. Unit data

  • The unit data type is similar to the structure data type, and also puts data of different attributes in a variable. The difference is that there are members under each element of the structure matrix, and each member has its own name. The references to the members are as follows:
    结构矩阵名.成员名
  • The elements of the unit matrix are different types of data, and the elements of the unit matrix are referenced in the form of subscripts with braces.
  • Building a unit matrix is ​​similar to a general matrix, except that the matrix elements are enclosed in curly braces.
>> a={
    
    10,'liu',[11,21;34,78];12,'wang',[34,191;27,578];14,'cai',[13,890;67,231]}

a =

  3×3 cell 数组

    {
    
    [10]}    {
    
    'liu' }    {
    
    2×2 double}
    {
    
    [12]}    {
    
    'wang'}    {
    
    2×2 double}
    {
    
    [14]}    {
    
    'cai' }    {
    
    2×2 double}

  • Cell matrix elements can be referenced using braced subscripts.
>> a{
    
    3,3}

ans =

    13   890
    67   231

  • Elements of a cell matrix can be structure or cell data. For example, we first establish the structure variable y, and assign values ​​to the element a{3,4} of the identity matrix a established above, the command is as follows:
>> y.x1=34;y.x2=56;
>> a{
    
    3,4}=y;
  • The entire cell matrix can be displayed using celldispthe function , such as celldisp(a). You can also delete an element in the unit matrix, such as deleting the third element of a, the command is as follows:
>> a(3)=[]

a =

  1×11 cell 数组

  列 18

    {
    
    [10]}    {
    
    [12]}    {
    
    'liu'}    {
    
    'wang'}    {
    
    'cai'}    {
    
    2×2 double}    {
    
    2×2 double}    {
    
    2×2 double}911

    {
    
    0×0 double}    {
    
    0×0 double}    {
    
    1×1 struct}

  • After the third element of the unit matrix a is deleted, a becomes a row vector. It should be noted that here is a(3), not a{3}. a{3}=[] sets the 3rd element of a to an empty matrix instead of deleting it.

Guess you like

Origin blog.csdn.net/weixin_45891612/article/details/130583564