load命令
Load命令存放数据文件的目录一般设置为d:\datafileload,导入*.txt格式的数据文件时可以先打开workspace窗口,在这个窗口的顶部有个工具按钮“Load
data file...”,通过这个打开你的filename.txt文件!
LOAD可以读MAT-file data或者用空格间隔的格式相似的ASCII data。matlab用load读取txt的文件,文件中必须是纯粹的数据,不允许有其他的文字说明。txt的数据文件最好是按矩阵形式排列,在读取之后可直接用于程序。
如% Load the file to the matrix, M :
M = load('sample_file.txt')
syms与sym的区别
syms是定义符号变量 sym是将字符或者数字转换为字符 比如 syms x y %就是定了符号变量x y以后x y就可以直接使用了,有他们运算出来的结果也是符号变量 当然上面的也可以x=sym('x'),y=sym('y') sys('a+b')%就是将a+b转化为符号表达式 两者有不同的使用场合 比如符号变量涉及的个数比较少,但是使用频率比较高,那么就是使用syms 但是如果个数多,但是使用频率比较少则使用sym
inline函数
在matlab命令窗口、程序或函数中创建局部函数时,可用inline。优点是不必将其储存为一个单独文件。在运用中有几点限制:不能调用另一个inline函数,只能由一个matlab表达式组成,并且只能返回一个变量—显然不允许[u,v]这种形式。因而,任何要求逻辑运算或乘法运算以求得最终结果的场合,都不能应用inline。除了这些限制,在许多情况下使用该函数非常方便。
Inline函数的一般形式为:
FunctionName=inline(‘任何有效的matlab表达式’, ‘p1’,’p2’ ,….) ,其中‘p1’,’p2’ ,…是出现在表达式中的所有变量的名字。
如:(求解F(x)=x^2*cos(a*x)-b ,a,b是标量;x是向量)
在命令窗口输入:
Fofx=inline(‘x .^2*cos(a*x)-b’ , ‘x’,’a’,’b’);
g= Fofx([pi/3 pi/3.5],4,1)
系统输出为:g=-1.5483 -1.7259
cat使用方法
norm函数
norm(A,'fro')是F-范数,norm(A,inf)是∞-范数,norm(A,1)1-范数
默认2范数
max函数
Return the maximum of a 2-by-3 matrix from each column:
X = [2 8 4; 7 3 9];
max(X,[],1)
ans =
7 8 9
Return the maximum from each row:
max(X,[],2)
ans =
8
9
Compare each element of X to a scalar:
max(X,5)
ans =
5 8 5
7 5 9
B = zeros(n) returns an n-by-n matrix of zeros. An error message appears if n is not a scalar.
B = zeros(m,n) or B = zeros([m n]) returns an m-by-n matrix of zeros.
B = zeros(m,n,p,...) or B = zeros([m n p ...]) returns an m-by-n-by-p-by-... array of zeros.
sum函数
B = sum(A)
B = sum(A,dim)
B = sum(..., 'double')
B = sum(..., dim,'double')
B = sum(..., 'native')
B = sum(..., dim,'native')
B = sum(A) returns sums along different dimensions of an array. If A is floating point, 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 point, 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.
B = sum(A,dim) sums along the dimension of A specified by scalar dim. The dim input is an integer value from 1 to N, where N is the number of dimensions in A. Set dim to 1 to compute the sum of each column, 2 to sum rows, etc.
B = sum(..., 'double') and B = sum(..., dim,'double') performs 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.
B = sum(..., 'native') and B = sum(..., dim,'native') performs 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.
If A = int8(1:20) then sum(A) accumulates in double and the result is double(210) while sum(A,'native') accumulates in int8, but overflows and saturates to int8(127).