数学建模--matlab基础知识 1. Matlab-入门篇:Hello world! 2. 基本运算 3.常量及变量 4.分支语句: 5.循环语句: 6.数据类型 7.运算符 格式命令

数学建模--matlab基础知识
1. Matlab-入门篇:Hello world!
2. 基本运算
3.常量及变量
4.分支语句:
5.循环语句:
6.数据类型
7.运算符
格式命令

虽然python也能做数据分析,不过参加数学建模,咱还是用专业的


程序员入门第一式:

disp(‘hello world!’)

2. 基本运算

先了解基本的运算符,做一些简单的尝试:

+   Plus; addition operator.

-   Minus; subtraction operator.

*   Scalar and matrix multiplication operator.

^   Scalar and matrix exponentiation operator.

/   Right-division operator.

:   Colon; generates regularly spaced elements and represents an entire row or column.

 [ ]    Brackets; enclosures array elements.

…  Ellipsis; line-continuation operator

;   Semicolon; separates columns and suppresses display.

%   Percent sign; designates a comment and specifies formatting.

+-*/都知道,^是幂运算

:   形成一个一个有规律间隔的序列:

1:2:10

 数学建模--matlab基础知识
1. Matlab-入门篇:Hello world!
2. 基本运算
3.常量及变量
4.分支语句:
5.循环语句:
6.数据类型
7.运算符
格式命令

 

…连接长语句,一行写不完,加…换到下一行写

 数学建模--matlab基础知识
1. Matlab-入门篇:Hello world!
2. 基本运算
3.常量及变量
4.分支语句:
5.循环语句:
6.数据类型
7.运算符
格式命令

 

% 注释

Command windows神器!!!:

doc 查找帮助文档

dos

现在,command窗口摇身一变,变成啥了呢?

 数学建模--matlab基础知识
1. Matlab-入门篇:Hello world!
2. 基本运算
3.常量及变量
4.分支语句:
5.循环语句:
6.数据类型
7.运算符
格式命令

数学建模--matlab基础知识
1. Matlab-入门篇:Hello world!
2. 基本运算
3.常量及变量
4.分支语句:
5.循环语句:
6.数据类型
7.运算符
格式命令

数学建模--matlab基础知识
1. Matlab-入门篇:Hello world!
2. 基本运算
3.常量及变量
4.分支语句:
5.循环语句:
6.数据类型
7.运算符
格式命令

是的,dos()函数和win+r的运行窗口一样一样的!

命令:

命令

目的/作用

clc

清除命令窗口。

clear

从内存中删除变量。

exist

检查存在的文件或变量。

global

声明变量为全局。

help

搜索帮助主题。

lookfor

搜索帮助关键字条目。

quit

停止MATLAB。

who

列出当前变量。(很好很强大,你值得拥有)

whos

列出当前变量(长显示)。

clc,clear,who,whos,help,quit是不是很强大呢,谁用谁知道

who 命令显示所有已经使用的变量名。

whos 命令显示多一点有关变量:

当前内存中的变量

每个变量的类型

内存分配给每个变量

无论他们是复杂的变量与否

clear命令删除所有(或指定)从内存中的变量(S)。

系统命令

MATLAB提供各种有用的命令与系统工作,在工作区中当前的工作,如保存为一个文件,并加载文件。

它还提供了其他系统相关的活动,如各种命令,显示日期,列出目录中的文件,显示当前目录等。

下表显示了一些常用的系统相关的命令:

命令

目的/作用

cd

改变当前目录。

date

显示当前日期。

delete

删除一个文件。

diary

日记文件记录开/关切换。

dir

列出当前目录中的所有文件。

load

负载工作区从一个文件中的变量。

path

显示搜索路径。

pwd

显示当前目录。

save

保存在一个文件中的工作区变量。

type

显示一个文件的​​内容。

what

列出所有MATLAB文件在当前目录中。

wklread

读取.wk1电子表格文件。 

Command窗口可以直接使用cd,dir这些命令,是不是很爽呢(其实dos(‘dir’也是可以的)但是,dos(‘cd ..’)并不能更换matlab command的路径)

Edit命令创建文件

3.常量及变量

常量:

ans Most recent answer.

eps Accuracy of floating-yiibai precision.

i,j The imaginary unit √-1.

Inf Infinity.(无穷大)

NaN Undefined numerical result (not a number).

pi  The number π(在处理sin(pi / 2)的时候,就是sin90度)

变量

在MATLAB环境下,每一个变量是一个数组或矩阵。

在一个简单的方法,您可以指定变量。例如,

x = 3         % defining x and initializing it with a value

MATLAB将执行上面的语句,并返回以下结果:

x =

     3

它创建了一个1-1的矩阵名为x和的值存储在其元素。让我们查看另一个例子,

x = sqrt(16) % defining x and initializing it with an expression

MATLAB将执行上面的语句,并返回以下结果:

x =

     4

请注意:

一旦一个变量被输入到系统中,你可以引用它。

变量在使用它们之前,必须有值。

当表达式返回一个结果,不分配给任何变量,系统分配给一个变量命名ans,以后可以使用。

sqrt(78)

MATLAB将执行上面的语句,并返回以下结果:

ans =

    8.8318

可以使用这个变量 ans:

9876/ans

MATLAB将执行上面的语句,并返回以下结果:

ans =

   1.1182e+03

4.分支语句:

语句

描述

if ... end statement

An if ... end statement consists of a boolean expression followed by one or more statements.

if...else...end statement

An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

If... elseif...elseif...else...end statements

An if statement can be followed by an (or more) optional elseif...and an else statement, which is very useful to test various condition.

nested if statements

You can use one if or elseif statement inside another if or elseif statement(s).

switch statement

switch statement allows a variable to be tested for equality against a list of values.

nested switch statements

You can use one swicth statement inside another switch statement(s).

 

在MATLAB 中 switch 语句的语法是:

switch <switch_expression>

   case <case_expression>

     <statements>

   case <case_expression>

     <statements>

     ...

     ...

   otherwise

       <statements>

end

5.循环语句:

while 循环

一个给定的条件为真时重复语句或语句组。测试条件才执行循环体。

for 循环

执行的语句序列多次缩写管理循环变量的代码。

nested 循环

可以使用一个或多个环路内任何另一个循环。

控制语句

描述

break 语句

终止循环语句,将执行的语句紧随循环。

continue 语句

导致循环,跳过它的身体的其余部分,并立即重新再次测试前的状况。

在MATLAB 中 while循环的语法是:

while <expression>

   <statements>

end

在MATLAB中的 for循环的语法是:

for index = values

  <program statements>

          ...

end

 

6.数据类型

Matlab是弱数据类型,可直接复制,先找一个值,再给他赋一个变量名

MATLAB 提供15个基本数据类型。每种数据类型的数据存储在矩阵或阵列的形式。这个矩阵的大小或阵列是一个最低 0-0,这可以长大为任何规模大小的矩阵或数组。

下表显示了在 MATLAB 中最常用的数据类型:

数据类型   描述

int8       8-bit signed integer

uint8  8-bit unsigned integer

int16  16-bit signed integer

uint16 16-bit unsigned integer

int32  32-bit signed integer

uint32 32-bit unsigned integer

int64  64-bit signed integer

uint64 64-bit unsigned integer

single single precision numerical data

double double precision numerical data

logical    logical values of 1 or 0, represent true and false respectively

char       character data (strings are stored as vector of characters)

cell array array of indexed cells, each capable of storing an array of a different dimension and data type

structure  C-like structures, each structure having named fields capable of storing an array of a different dimension and data type

function handle   yiibaier to a function

下面这两个说明我们可以调用自己定义的类和java文件

user classes  objects constructed from a user-defined class

java classes  objects constructed from a Java class

7.运算符

运算符是一个符号,它告诉编译器执行特定的数学或逻辑操作。 MATLAB 设计工作主要是对整个矩阵和阵列。因此,运算符在 MATLAB 工作标和非标量数据。 MATLAB 允许以下类型的基本运算:

l  算术运算符

l  关系运算符

l  逻辑运算符

l  位运算

l  集合运算

算术运算符

MATLAB允许两种不同类型的算术运算:

  • 矩阵算术运算
  • 阵列算术运算

运算符

描述

+

加法或一元加号。A + B将A和B。 A和B必须具有相同的尺寸,除非一个人是一个标量。一个标量,可以被添加到任何大小的矩阵。

-

Subtraction or unary minus. A-B subtracts B from A. A and B must have the same size, unless one is a scalar. A scalar can be subtracted from a matrix of any size.

*

Matrix multiplication. C = A*B is the linear algebraic product of the matrices A and B. More precisely,

For nonscalar A and B, the number of columns of A must equal the number of rows of B. A scalar can multiply a matrix of any size.

.*

Array multiplication. A.*B is the element-by-element product of the arrays A and B. A and B must have the same size, unless one of them is a scalar.

/

Slash or matrix right division. B/A is roughly the same as B*inv(A). More precisely, B/A = (A'B')'.

./

Array right division. A./B is the matrix with elements A(i,j)/B(i,j). A and B must have the same size, unless one of them is a scalar.

 

Backslash or matrix left division. If A is a square matrix, AB is roughly the same as inv(A)*B, except it is computed in a different way. If A is an n-by-n matrix and B is a column vector with n components, or a matrix with several such columns, then X = AB is the solution to the equation AX = B. A warning message is displayed if A is badly scaled or nearly singular.

.

Array left division. A.B is the matrix with elements B(i,j)/A(i,j). A and B must have the same size, unless one of them is a scalar.

^

Matrix power. X^p is X to the power p, if p is a scalar. If p is an integer, the power is computed by repeated squaring. If the integer is negative, X is inverted first. For other values of p, the calculation involves eigenvalues and eigenvectors, such that if [V,D] = eig(X), then X^p = V*D.^p/V.

.^

Array power. A.^B is the matrix with elements A(i,j) to the B(i,j) power. A and B must have the same size, unless one of them is a scalar.

'

Matrix transpose. A' is the linear algebraic transpose of A. For complex matrices, this is the complex conjugate transpose.

.'

Array transpose. A.' is the array transpose of A. For complex matrices, this does not involve conjugation.

算术运算功能

除了在上述的算术运算符,MATLAB 用于类似的目的提供了以下的命令/功能:

函数

描述

uplus(a)

Unary plus; increments by the amount a

plus (a,b)

Plus; returns a + b

uminus(a)

Unary minus; decrements by the amount a

minus(a, b)

Minus; returns a - b

times(a, b)

Array multiply; returns a.*b

mtimes(a, b)

Matrix multiplication; returns a* b

rdivide(a, b)

Right array division; returns a ./ b

ldivide(a, b)

Left array division; returns a. b

mrdivide(A, B)

Solve systems of linear equations xA = B for x

mldivide(A, B)

Solve systems of linear equations Ax = B for x

power(a, b)

Array power; returns a.^b

mpower(a, b)

Matrix power; returns a ^ b

cumprod(A)

Cumulative product; returns an array the same size as the array A containing the cumulative product.

  • If A is a vector, then cumprod(A) returns a vector containing the cumulative product of the elements of A.
  • If A is a matrix, then cumprod(A) returns a matrix containing the cumulative products for each column of A.
  • If A is a multidimensional array, then cumprod(A) acts along the first nonsingleton dimension.

cumprod(A, dim)

Returns the cumulative product along dimension dim.

cumsum(A)

Cumulative sum; returns an array A containing the cumulative sum.

  • If A is a vector, then cumsum(A) returns a vector containing the cumulative sum of the elements of A.
  • If A is a matrix, then cumsum(A) returns a matrix containing the cumulative sums for each column of A.
  • If A is a multidimensional array, then cumsum(A) acts along the first nonsingleton dimension.

cumsum(A, dim)

returns the cumulative sum of the elements along dimension dim.

diff(X)

Differences and approximate derivatives; calculates differences between adjacent elements of X.

  • If X is a vector, then diff(X) returns a vector, one element shorter than X, of differences between adjacent elements: [X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]
  • If X is a matrix, then diff(X) returns a matrix of row differences: [X(2:m,:)-X(1:m-1,:)]

diff(X,n)

Applies diff recursively n times, resulting in the nth difference.

diff(X,n,dim)

It is the nth difference function calculated along the dimension specified by scalar dim. If order n equals or exceeds the length of dimension dim, diff returns an empty array.

prod(A)

Product of array elements; returns the product of the array elements of A.

  • If A is a vector, then prod(A) returns the product of the elements.
  • If A is a nonempty matrix, then prod(A) treats the columns of A as vectors and returns a row vector of the products of each column.
  • If A is an empty 0-by-0 matrix, prod(A) returns 1.
  • If A is a multidimensional array, then prod(A) acts along the first nonsingleton dimension and returns an array of products. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.

The prod function computes and returns B as single if the input, A, is single. For all other numeric and logical data types, prod computes and returns B as double

prod(A,dim)

Returns the products along dimension dim. For example, if A is a matrix, prod(A,2) is a column vector containing the products of each row.

prod(___,datatype)

multiplies in and returns an array in the class specified by datatype.

sum(A)

  • Sum of array elements; returns sums along different dimensions of an array. If A is floating yiibai, that is double or single, B is accumulated natively, that is in the same class as A, and B has the same class as A. If A is not floating yiibai, B is accumulated in double and B has class double.
  • If A is a vector, sum(A) returns the sum of the elements.
  • If A is a matrix, sum(A) treats the columns of A as vectors, returning a row vector of the sums of each column.
  • If A is a multidimensional array, sum(A) treats the values along the first non-singleton dimension as vectors, returning an array of row vectors.

sum(A,dim)

Sums along the dimension of A specified by scalar dim.

sum(..., 'double')

sum(..., dim,'double')

Perform additions in double-precision and return an answer of type double, even if A has data type single or an integer data type. This is the default for integer data types.

sum(..., 'native')

sum(..., dim,'native')

Perform additions in the native data type of A and return an answer of the same data type. This is the default for single and double.

ceil(A)

Round toward positive infinity; rounds the elements of A to the nearest integers greater than or equal to A.

fix(A)

Round toward zero

floor(A)

Round toward negative infinity; rounds the elements of A to the nearest integers less than or equal to A.

idivide(a, b)

idivide(a, b,'fix')

Integer division with rounding option; is the same as a./b except that fractional quotients are rounded toward zero to the nearest integers.

idivide(a, b, 'round')

Fractional quotients are rounded to the nearest integers.

idivide(A, B, 'floor')

Fractional quotients are rounded toward negative infinity to the nearest integers.

idivide(A, B, 'ceil')

Fractional quotients are rounded toward infinity to the nearest integers.

mod (X,Y)

Modulus after division; returns X - n.*Y where n = floor(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars (provided Y ~=0).

Please note:

  • mod(X,0) is X
  • mod(X,X) is 0
  • mod(X,Y) for X~=Y and Y~=0 has the same sign as Y

rem (X,Y)

Remainder after division; returns X - n.*Y where n = fix(X./Y). If Y is not an integer and the quotient X./Y is within roundoff error of an integer, then n is that integer. The inputs X and Y must be real arrays of the same size, or real scalars(provided Y ~=0).

Please note that:

  • rem(X,0) is NaN
  • rem(X,X) for X~=0 is 0
  • rem(X,Y) for X~=Y and Y~=0 has the same sign as X.

round(X)

Round to nearest integer; rounds the elements of X to the nearest integers. Positive elements with a fractional part of 0.5 round up to the nearest positive integer. Negative elements with a fractional part of -0.5 round down to the nearest negative integer.

关系运算符

关系运算符标和非标量数据上也能正常工作。关系运算符对数组进行元素元素元素设置为逻辑1(真)的关系是真实的和元素设置为逻辑0(假),它是两个阵列,并返回一个同样大小的逻辑阵列之间的比较。

函数

描述

eq(a, b)

Tests whether a is equal to b

ge(a, b)

Tests whether a is greater than or equal to b

gt(a, b)

Tests whether a is greater than b

le(a, b)

Tests whether a is less than or equal to b

lt(a, b)

Tests whether a is less than b

ne(a, b)

Tests whether a is not equal to b

isequal

Tests arrays for equality

isequaln

Tests arrays for equality, treating NaN values as equal

下表显示了 MATLAB 中的关系运算符:

运算符

描述

< 

Less than

<=

Less than or equal to

> 

Greater than

>=

Greater than or equal to

==

Equal to

~=

Not equal to

逻辑运算符

MATLAB提供了两种类型的逻辑运算符和函数:

  • Element-wise -这些运算符的逻辑阵列上运行相应的元素。
  • Short-circuit -这些运算上的标量,逻辑表达式。

Element-wise 的逻辑运算符操作元素元素逻辑阵列。符号&,|和〜逻辑数组运算符AND,OR,NOT。

允许短路短路逻辑运算符,逻辑运算。符号 && 和 | | 是短路逻辑符 AND 和 OR。

除了在上述的逻辑运算符,MATLAB 提供下面的命令或函数用于同样的目的:

函数

描述

and(A, B)

Finds logical AND of array or scalar inputs; performs a logical AND of all input arrays A, B, etc. and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to 1 if all input arrays contain a nonzero element at that same array location. Otherwise, that element is set to 0.

not(A)

Finds logical NOT of array or scalar input; performs a logical NOT of input array A and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to 1 if the input array contains a zero value element at that same array location. Otherwise, that element is set to 0.

or(A, B)

Finds logical OR of array or scalar inputs; performs a logical OR of all input arrays A, B, etc. and returns an array containing elements set to either logical 1 (true) or logical 0 (false). An element of the output array is set to 1 if any input arrays contain a nonzero element at that same array location. Otherwise, that element is set to 0.

xor(A, B)

Logical exclusive-OR; performs an exclusive OR operation on the corresponding elements of arrays A and B. The resulting element C(i,j,...) is logical true (1) if A(i,j,...) or B(i,j,...), but not both, is nonzero.

all(A)

Determine if all array elements of array A are nonzero or true.

  • If A is a vector, all(A) returns logical 1 (true) if all the elements are nonzero and returns logical 0 (false) if one or more elements are zero.
  • If A is a nonempty matrix, all(A) treats the columns of A as vectors, returning a row vector of logical 1's and 0's.
  • If A is an empty 0-by-0 matrix, all(A) returns logical 1 (true).
  • If A is a multidimensional array, all(A) acts along the first nonsingleton dimension and returns an array of logical values. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.

all(A, dim)

Tests along the dimension of A specified by scalar dim.

any(A)

Determine if any array elements are nonzero; tests whether any of the elements along various dimensions of an array is a nonzero number or is logical 1 (true). The any function ignores entries that are NaN (Not a Number).

  • If A is a vector, any(A) returns logical 1 (true) if any of the elements of A is a nonzero number or is logical 1 (true), and returns logical 0 (false) if all the elements are zero.
  • If A is a nonempty matrix, any(A) treats the columns of A as vectors, returning a row vector of logical 1's and 0's.
  • If A is an empty 0-by-0 matrix, any(A) returns logical 0 (false).
  • If A is a multidimensional array, any(A) acts along the first nonsingleton dimension and returns an array of logical values. The size of this dimension reduces to 1 while the sizes of all other dimensions remain the same.

any(A,dim)

Tests along the dimension of A specified by scalar dim.

false

Logical 0 (false)

false(n)

is an n-by-n matrix of logical zeros

false(m, n)

is an m-by-n matrix of logical zeros.

false(m, n, p, ...)

is an m-by-n-by-p-by-... array of logical zeros.

false(size(A))

is an array of logical zeros that is the same size as array A.

false(...,'like',p)

is an array of logical zeros of the same data type and sparsity as the logical array p.

ind = find(X)

Find indices and values of nonzero elements; locates all nonzero elements of array X, and returns the linear indices of those elements in a vector. If X is a row vector, then the returned vector is a row vector; otherwise, it returns a column vector. If X contains no nonzero elements or is an empty array, then an empty array is returned.

ind = find(X, k)

ind = find(X, k, 'first')

Returns at most the first k indices corresponding to the nonzero entries of X. k must be a positive integer, but it can be of any numeric data type.

ind = find(X, k, 'last')

returns at most the last k indices corresponding to the nonzero entries of X.

[row,col] = find(X, ...)

Returns the row and column indices of the nonzero entries in the matrix X. This syntax is especially useful when working with sparse matrices. If X is an N-dimensional array with N > 2, col contains linear indices for the columns.

[row,col,v] = find(X, ...)

Returns a column or row vector v of the nonzero entries in X, as well as row and column indices. If X is a logical expression, then v is a logical array. Output v contains the non-zero elements of the logical array obtained by evaluating the expression X.

islogical(A)

Determine if input is logical array; returns true if A is a logical array and false otherwise. It also returns true if A is an instance of a class that is derived from the logical class.

logical(A)

Convert numeric values to logical; returns an array that can be used for logical indexing or logical tests.

true

Logical 1 (true)

true(n)

is an n-by-n matrix of logical ones.

true(m, n)

is an m-by-n matrix of logical ones.

true(m, n, p, ...)

is an m-by-n-by-p-by-... array of logical ones.

true(size(A))

is an array of logical ones that is the same size as array A.

true(...,'like', p)

is an array of logical ones of the same data type and sparsity as the logical array p.

位运算

位运算符位和执行位位操作。 &,|和^的真值表如下:

p

q

p & q

p | q

p ^ q

0

0

0

0

0

0

1

0

1

1

1

1

1

1

0

1

0

0

1

1

假设如果A= 60,B =13,他们现在以二进制格式将如下:

A = 0011 1100

B = 0000 1101

-----------------

A&B = 0000 1100

A|B = 0011 1101

A^B = 0011 0001

~A  = 1100 0011

MATLAB提供位运算,如'位','位'和'位不操作,移位操作等各种函数

以下的表格显示了常用的按位运算:

函数

目的/作用

bitand(a, b)

Bit-wise AND of integers a and b

bitcmp(a)

Bit-wise complement of a

bitget(a,pos)

Get bit at specified position pos, in the integer array a

bitor(a, b)

Bit-wise OR of integers a and b

bitset(a, pos)

Set bit at specific location pos of a

bitshift(a, k)

Returns a shifted to the left by k bits, equivalent to multiplying by 2k. Negative values of k correspond to shifting bits right or dividing by 2|k| and rounding to the nearest integer towards negative infinite. Any overflow bits are truncated.

bitxor(a, b)

Bit-wise XOR of integers a and b

swapbytes

Swap byte ordering

MATLAB提供各种功能集合运算,如集,交集和测试组成员等。

下表显示了一些常用的设置操作:

函数

描述

intersect(A,B)

Set intersection of two arrays; returns the values common to both A and B. The values returned are in sorted order.

intersect(A,B,'rows')

Treats each row of A and each row of B as single entities and returns the rows common to both A and B. The rows of the returned matrix are in sorted order.

ismember(A,B)

Returns an array the same size as A, containing 1 (true) where the elements of A are found in B. Elsewhere, it returns 0 (false).

ismember(A,B,'rows')

Treats each row of A and each row of B as single entities and returns a vector containing 1 (true) where the rows of matrix A are also rows of B. Elsewhere, it returns 0 (false).

issorted(A)

Returns logical 1 (true) if the elements of A are in sorted order and logical 0 (false) otherwise. Input A can be a vector or an N-by-1 or 1-by-N cell array of strings. A is considered to be sorted if A and the output of sort(A) are equal.

issorted(A, 'rows')

Returns logical 1 (true) if the rows of two-dimensional matrix A are in sorted order, and logical 0 (false) otherwise. Matrix A is considered to be sorted if A and the output of sortrows(A) are equal.

setdiff(A,B)

Set difference of two arrays; returns the values in A that are not in B. The values in the returned array are in sorted order.

setdiff(A,B,'rows')

Treats each row of A and each row of B as single entities and returns the rows from A that are not in B. The rows of the returned matrix are in sorted order.

The 'rows' option does not support cell arrays.

setxor

Set exclusive OR of two arrays

union

Set union of two arrays

unique

Unique values in array

集合运算:

函数

描述

intersect(A,B)

Set intersection of two arrays; returns the values common to both A and B. The values returned are in sorted order.

intersect(A,B,'rows')

Treats each row of A and each row of B as single entities and returns the rows common to both A and B. The rows of the returned matrix are in sorted order.

ismember(A,B)

Returns an array the same size as A, containing 1 (true) where the elements of A are found in B. Elsewhere, it returns 0 (false).

ismember(A,B,'rows')

Treats each row of A and each row of B as single entities and returns a vector containing 1 (true) where the rows of matrix A are also rows of B. Elsewhere, it returns 0 (false).

issorted(A)

Returns logical 1 (true) if the elements of A are in sorted order, and logical 0 (false) otherwise. Input A can be a vector or an N-by-1 or 1-by-N cell array of strings. A is considered to be sorted if A and the output of sort(A) are equal.

issorted(A, 'rows')

Returns logical 1 (true) if the rows of two-dimensional matrix A are in sorted order, and logical 0 (false) otherwise. Matrix A is considered to be sorted if A and the output of sortrows(A) are equal.

setdiff(A,B)

Set difference of two arrays; returns the values in A that are not in B. The values in the returned array are in sorted order.

setdiff(A,B,'rows')

Treats each row of A and each row of B as single entities and returns the rows from A that are not in B. The rows of the returned matrix are in sorted order.

The 'rows' option does not support cell arrays.

setxor

Set exclusive OR of two arrays

union

Set union of two arrays

unique

Unique values in array

格式命令

默认情况下,MATLAB 四个小数位值显示数字。这就是所谓的 short format.

format long e(科学计数法显示结果)

但是,如果想更精确,需要使用 format 命令。

 

长(long ) 命令格式显示小数点后16位。

format long 16

short   4

bank    2

format rat 格式大鼠命令给出最接近的有理表达式,从计算所得。例如,

format rat

4.678 * 4.9

MATLAB将执行上面的语句,并返回以下结果:

ans =

   2063/90 

输入和输出命令

MATLAB提供了以下输入和输出相关的命令:

命令

作用/目的

disp

显示一个数组或字符串的内容。

fscanf

阅读从文件格式的数据。

format

控制屏幕显示的格式。

fprintf

执行格式化写入到屏幕或文件。

input

显示提示并等待输入。

;

禁止显示网版印刷

fscanf和fprintf命令的行为像C scanf和printf函数。他们支持格式如下代码:

格式代码

目的/作用

%s

Format as a string.

%d

Format as an integer.

%f

Format as a floating yiibai value.

%e

Format as a floating yiibai value in scientific notation.

%g

Format in the most compact form: %f or %e.

 

Insert a new line in the output string.

 

Insert a tab in the output string.

用于数字显示格式的函数有以下几种形式:

Format函数

最多可显示

format short

Four decimal digits (default).

format long

16 decimal digits.

format short e

Five digits plus exponent.

format long e

16 digits plus exponents.

format bank

Two decimal digits.

format +

Positive, negative, or zero.

format rat

Rational approximation.

format compact

Suppresses some line feeds.

format loose

Resets to less compact display mode.

向量,矩阵和阵列命令

下表列出了各种命令用于工作数组,矩阵和向量:

命令

作用/目的

cat

Concatenates arrays.连接数组

find

Finds indices of nonzero elements.

length

Computes number of elements.

linspace

Creates regularly spaced vector.

logspace

Creates logarithmically spaced vector.

max

Returns largest element.

min

Returns smallest element.

prod

Product of each column.

reshape

Changes size.

size

Computes array size.

sort

Sorts each column.

sum

Sums each column.

eye

Creates an identity matrix. 创建单位矩阵

ones

Creates an array of ones. 创建一个1的数组

zeros

Creates an array of zeros. 创建一个0数组

cross

Computes matrix cross products.计算矩阵交叉积

dot

Computes matrix dot products. 点积

det

Computes determinant of an array.计算行列式

inv

Computes inverse of a matrix.计算行列式的逆

pinv

Computes pseudoinverse of a matrix.计算行列式的违逆

rank

Computes rank of a matrix.计算行列式的秩

rref

Computes reduced row echelon form.

cell

Creates cell array.

celldisp

Displays cell array.

cellplot

Displays graphical representation of cell array.

num2cell

Converts numeric array to cell array.

deal

Matches input and output lists.

iscell

Identifies cell array.

MATLAB提供了大量的命令,绘制图表。下表列出了一些常用的命令绘制:

命令

作用/目的

axis

Sets axis limits.

fplot

Intelligent plotting of functions.

grid

Displays gridlines.

plot

Generates xy plot.

print

Prints plot or saves plot to a file.

title

Puts text at top of plot.

xlabel

Adds text label to x-axis.

ylabel

Adds text label to y-axis.

axes

Creates axes objects.

close

Closes the current plot.

close all

Closes all plots.

figure

Opens a new figure window.

gtext

Enables label placement by mouse.

hold

Freezes current plot.

legend

Legend placement by mouse.

refresh

Redraws current figure window.

set

Specifies properties of objects such as axes.

subplot

Creates plots in subwindows.

text

Places string in figure.

bar

Creates bar chart.

loglog

Creates log-log plot.

polar

Creates polar plot.

semilogx

Creates semilog plot. (logarithmic abscissa).

semilogy

Creates semilog plot. (logarithmic ordinate).

stairs

Creates stairs plot.

stem

Creates stem plot.