matlab array operation and programming of an array

in matlab arithmetic operations and mainly targeted the array, and the array comprising: an array of values, character array, cellular array like.

First, establish the value of the array:

1. Direct Input:

Commas: used to separate the row elements in the array. (Available spaces instead)

Semicolon: used to distinguish the array row. (Available instead of the Enter key)

Square brackets []: Definition head and tail arrays.

a=[1,2,3,8,-1], 
b=[1;2;3;8;-1],
A= [2,4,1;0,-2,4;2,4,6]

2. Editor generation matrix array by    

Steps: first create a null matrix = [], and then in the working space (Workspace) into the midpoint of opening a list editor, input elements.

3. Data introduction method:            

matlab excel file can be directly imported in the main interface, the data in the plain text file.

4. Create an array with function

Generating a fixed-step method: x = a: t: b (t step, 1 is omitted);

Linear sampling set number: x = linspace (a, b, n), a and b are the first and last element of the array, n is the total number of points sampled.

zeros (m): m-order all-zero matrix

zeros (m, n): m × n-order all-zero matrix

eye (m): m-order unit matrix

eye (m): m-order unit matrix

ones (m, n): m × n matrix-order all-1

rand (m): m Order [0,1] uniformly distributed random matrix

randn (m): m-order standard normal random matrix

rand(m,n), randn(m,n)

Second, the array of characters, an array of cellular established:    

Input character array: an array of input characters enclosed in single quotes, such as: A = 'matlab'

Cellular arrays MATLAB is a special data type, the cellular array can be seen as an all-embracing general matrix, or called generalized matrix. Cellular elements of the array may be any amount of a data type, each element may have different sizes, the content of each element may be a completely different cellular elements in the array is called a lane.

The establishment of cellular arrays:

Commas: used to separate the row elements in the array. (Available spaces instead)

Semicolon: used to distinguish the array row. (Available instead of the Enter key)

Braces {}: Definition head and tail arrays.

Such as: a = { 'matlab', 20; ones (2,3), 1: 10}

Second, the operation of the array:       

Addressing the array: the array a build, in a method for addressing the respective elements as follows:        

Single index addressing: a (1) represented by a first element, a (n) represents a n-th element. For a single two-dimensional array subscript addressed by the principle of priority list.        

Double subscript addressed: a (2,3) matrix is ​​represented by a second line of three elements.

1. Extraction with array element subarray    

Extracting a first array of three elements: y = a (3)    

Extraction of a 3-7 elements: y = a (3: 7),   

format 
a=linspace(1,20,7)
b=a(1:3:5)
b1=a(5:-3:2)
b2=a(a>1)

A two-dimensional array of elements of extraction:        

Since the array A has two addressing methods, matlab based on the received command, is first determined which method of addressing, and then extracted elements.

Such as: 

A=[1,2,3,4,5;0,1,2,3,4;-1,0,1,2,3;-2,-1,0,1,2] 
b1=A(5) 
b2=A(2,3)
b3=A(2,:)
b4=A([2,3])
b5=[A(2,:);A(1,:)]
b6=A([2,4],[3,5]) 
b7=A([2,4],[3,5,1])

2. Change the splice element values ​​in the array and the array

a=1:2:11
a(1)=0
a(1:4)=[2,-1,-2,-3]
a([2,5])=[1.5,0.5]
x=0:2:10, 
y=[-2,-5,-8]
xx=[x,y]
yy=[xx([2,5]);y(2:3)] 

Empty array of use:

Create an empty array A: A = [], empty array of any size.

Empty array elements can be used to delete an existing array

B=1:8

B(1:2:5)=[]

B =   2     4     6     7     8

A=[2,3,4,5,6;1,2,3,4,5;0,1,2,3,4;-1,0,1,2,3]

A delete matrix row 3: A (3,:) = []

Delete Matrix A second 2: A (:, 2) = []

3. Common array operation command

(1) determining the size of the array command    

A maximum number of rows to identify the array and the number of columns: n = length (A)    

The number of rows and columns of the array A Extraction: [m, n] = size (A)    

To find the total number of elements in array A: N = numel (A);

(2) sorting to sort command element one-dimensional array of x: x = [3, -1,2,5,7,4,6,11,13,9,2,0,7,8] b = sort ( x), [b, k] = sort (x)

(3) changing the command shape array x = [3, -1,2,5,7,4,6,11,13,9,2,8] one-dimensional array x conditional conversion matrix: B = reshape (x, 3,4)

(4) Copy of the array: c = [1,2,5] c1 = repmat (c, 4,1) c2 = repmat (c, 1,4) c3 = repmat (c, 3,2) c4 = repmat ( c, 3)

(5) sparse matrix with full matrix transformation:    

Sparse matrix generating command: sparse (a, b, c) an array of a, b, c of size must be the same as the array a and b specified elements are row markers and column labels, the array c of the specified element value A = sparse ([2, 4, 18], [3,12,20], [- 5 and -3, -8]) to create the sparse matrix a, a (2,3), (4,12), (18,20) each element -5, -3, -8, and the remaining elements are zero, A is a 18 × 20 matrix of order. The sparse matrix x back full matrix: A = full (A)

(6) sum (A): A matrix column by summing returns a row vector;      

sum (A, 2): A matrix row sums, returns a column vector.      

max (A): Returns a vector of a matrix constituted by the maximum value of each column.      

max (A, B): Returns the matrix A corresponding to the maximum configuration of the element B      

min (A), min (A, B) 类似

(7) diag command

b = diag (A): Extraction of the diagonal elements constituting the matrix A column vector b

A = diag (b): the diagonal matrix A generated by the elements of the one-dimensional array b

A = diag (b, k): (b-dimensional arrays, k is an integer) as the element b k-th diagonal offset from the main diagonal square matrix A generated

b=[2,3,-1,5,6],   A=diag(b,1)   B=diag(b,-2)

 (8) find command:      

Find (A) does not find an element A subscript 0      

find (A, k) to identify the top-k is not 0, the elements of the subscript A      

After find (A, k, 'last') to find the A element is not the k subscript 0      

Find (g (A)), where g (A) is a logical expression of the array A, array A return satisfies the condition g (A) of the index element.

      A=[0,0,2,-1,3,0,0,5,0,6,-7,0,0,9];
      b1=find(A,3)
      b2=find(A,2, 'last')  
B=[0,1,0;2,3,0;4,0,0]
 c1=find(B)
 [m,n]=find(B)

 B=[0,1,0;2,3,0;4,0,0]  
 t=find(B>2) 
[x,y]=find(B>2) 
[m,n]=find(B>=1&B<=3)

(9) permutations

Seeking a combination nchoosek (a, k) D = nchoosek (5,2)  

  a=[3,2,1,7];  M=nchoosek(a,2)

b='wxyz'  N=nchoosek(b,3)

Seeking a product even c1 = prod (4: 6) c2 = cumprod (4: 6)

Seeking full permutation vector perms ([2,1,8])

 (10) Other commands:      

fliplr: matrix inversion around      

flipud: The Matrix upside down      

rot90: counterclockwise rotation matrix      

tril: main lower triangular matrix of partially extracted      

triu: extracting the main triangular part of the matrix

 clc,clear
 a=[8,4,3,2
      4,6,2,2
      3,6,2,1
      2,2,0,7]
 b1=fliplr(a)
 a, b2=rot90(a)
 a, b3=tril(a)

11. The operation of cellular extracts array elements:

() {} And are essentially different, {} indicates the content of elementary cells, () represents the specified lane.

Third, the operation of the array

Numerical calculation value calculation array 1. The array, also known as the dot operator, is of the same order corresponding to the array operation between the components. Comprising a dot, and the point addition power point, the corresponding operation symbol. *, ./. ^ Common feature of these operators is a plus operator periods prior to the matrix operation corresponding to distinguish. Let A and B are of the order of the same array, k is a constant, A + B, AB, k * A A. * B, A./B, A. ^ n    

则:a+b= [a1+b1,a2+b2,…,an+bn]    

a.*b= [a1*b1,a2*b2,…,an*bn]    

a./b= [a1 / b1, a2 / b2, ..., an / bn]    

a.\b=[b1/a1,b2/a2,…,bn/an]    

a.^b=[a1^b1,a2^b2,…,an^bn]

2. Relationship between the ALU operation relational operators: not equal: ~ = 

3. The array set operations

 a = 1: 12; b = 2: 10; setdiff (a, b) (a and b set difference) is obtained: 1,11,12

intersect (a, b) (a and b the intersection)

Too: 8, 9,

union (a, b) (a and b and set)

Published 591 original articles · won praise 76 · views 50000 +

Guess you like

Origin blog.csdn.net/mmk27_word/article/details/104065215