Reprint common operations of MATLAB matrix Array

Source: Internet
Author: User

I,

Length returns the length of the longest dimension of the matrix. ndims returns the dimension. numel returns the number of matrix elements.
Size returns the length of each dimension. [rows, cols] = size ()

Matrix block operations

1. repmat () array block copy

2. blkdiag () diagonal block generation function

3. Kron () Direct Product

Ii. Extraction and replacement of Matrix Elements

(1) Extraction

A (I, j): Elements in matrix A that are in the column I and in the column J.
Use the colon (:) to retrieve an entire column or a whole row.
Use the reserved word "end" to represent the maximum value of a dimension. 
A (:, end)-the last straight line of matrix
A (n, :): extract the element of a row in the matrix,

Extract a column in the matrix: A (:, n)

Extract the multi-row/column element a ([n, m], :) A (:, [n, m]) in the matrix.

Extract the elements at the intersection of multiple rows and columns in the matrix.

(2) Replacement/Deletion:

A (2, :) = []-delete the second column of matrix
A (:, [2 4 5]) = []-delete the second, fourth, and fifth rows of a Matrix
Iii. Arrangement of Matrix Elements

1) Arrange Matrix Elements

B = reshape (a, m, n): returns an M * n matrix B. Elements of matrix B are elements of matrix, if the number of elements in matrix A is not M * n, an error is returned.

B = reshape (a, m, n, p): returns a multidimensional array B. The number of elements in array B is equal to the number of elements in matrix.

B = reshape (,..., [],…) : A dimension can be set by default.

B = reshape (A, siz): the dimension of array B is specified by the vector siz. The product of each element of siz must be equal to the number of elements of matrix.


2) the copy and arrangement function of the matrix is repmat.

B = repmat (A, n): returns B as a matrix of N x n blocks, each of which is

B = repmat (a, m, n): the return value is a large matrix composed of M * n blocks. Each block is a matrix.

B = repmat (A, [M, N, P,…]) : Return Value B is a block in the form of a multi-dimensional array. Each block is a matrix.


3) flip and rotate the Matrix
Rot90 rotation matrix 90 degrees, counterclockwise
Fliplr flip matrix along vertical axis
Flipud flud matrix along the horizontal axis
Transpose flipped matrix along the main diagonal line

Iv. vector creation

1) enter directly:

2) generate a vector ":"

A = J: the row vector generated by K is a = [J, J + 1 ,..., K]

A = J: D: K generate row vector A = [J, J + D ,..., J + M * D], M = fix (K-J)/D)

3) The linspace function is used to generate a row vector in the form of an equal deviation.

X = linspace (x1, x2): 100 linear distribution data are generated between X1 and X2, and the difference between the two adjacent data remains unchanged. Form an equal difference sequence.

X = linspace (x1, x2, n): Generate n linear distribution data between X1 and X2, and the difference between the two adjacent data remains unchanged. Form an equal difference sequence.

4) The function logspace is used to generate a line vector in the proportional form.

X = logspace (x1, x2) generates 50 row vectors of logarithm-like data between X1 and X2. The first X (1) = 10x1, x (50) = 10x2

X = logspace (x1, x2, n) generates n row vectors of logarithm-equals data between X1 and X2. Returns the first X (1) = 10x1, x (n) = 10x2.

Reprint common operations of MATLAB matrix Array

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.