matlab的常用函数


本文主要介绍在使用matlab过程中遇到的一些常用函数,其中和数值有关的函数

round 、fix、floor、ceil

和矩阵有关的函数

inv

和画图有关的函数

plot

和运算有关的函数

max , power , mpower

round

官方文档

round  rounds towards nearest decimal or integer

round(X) rounds each element of X to the nearest integer.

round(X, N), for positive integers N, rounds to N digits to the right
of the decimal point. If N is zero, X is rounded to the nearest integer.
If N is less than zero, X is rounded to the left of the decimal point.
N must be a scalar integer.

round(X, N, 'significant') rounds each element to its N most significant
digits, counting from the most-significant or left side of the number. 
N must be a positive integer scalar.

round(X, N, 'decimals') is equivalent to round(X, N).

For complex X, the imaginary and real parts are rounded independently.

Examples
--------
% Round pi to the nearest hundredth
>> round(pi, 2)
     3.14

% Round the equatorial radius of the Earth, 6378137 meters,
% to the nearest kilometer.
round(6378137, -3)
     6378000

% Round to 3 significant digits
format shortg;
round([pi, 6378137], 3, 'significant')
     3.14     6.38e+06

If you only need to display a rounded version of X,
consider using fprintf or num2str:

fprintf('%.3f\n', 12.3456)
     12.346 
fprintf('%.3e\n', 12.3456)
     1.235e+01

Examples

>> round(123.1234) % 默认保留到整数

ans =

   123

>> round(123.2134, 3) % 保留三位小数

ans =

  123.2130

>> round(123.2134, -1) % 保留到小数点左边第1位

ans =

   120

>> round(123.2134, 1 , 'significant') % 从最高位(最左边)开始,保留1位

ans =

   100

fix

官方文档

fix    Round towards zero.
	fix(X) rounds the elements of X to the nearest integers
	towards zero.

floor

官方文档

floor  Round towards minus infinity.  % 朝向负无穷大
		floor(X) rounds the elements of X to the nearest integers
 	 towards minus infinity.

ceil

官方文档

ceil   Round towards plus infinity.
	ceil(X) rounds the elements of X to the nearest integers
	 towards infinity.

数值数据转换函数总结

函数作用
roundrounds towards nearest decimal or integer
fixRound towards zero
floorRound towards minus infinity.
ceilRound towards plus infinity.

inv

官方文档

inv    Matrix inverse.
	    inv(X) is the inverse of the square matrix X.
	    A warning message is printed if X is badly scaled or
	    nearly singular.

Exemples

>> A = magic(3);
>> inv(A) * A

ans =

    1.0000         0   -0.0000
         0    1.0000         0
         0    0.0000    1.0000

plot

2-D 绘图

plot(X,Y)

官方文档

plot(X,Y) creates a 2-D line plot of the data in Y versus the corresponding values in X.	
If X and Y are both vectors,
	 then they must have equal length. The plot function plots Y versus X.	
If X and Y are both matrices, 
	then they must have equal size. The plot function plots columns of Y versus columns of X.	
If one of X or Y is a vector and the other is a matrix, 
	then the matrix must have dimensions such that one of its dimensions 
	equals the vector length. 
	
	If the number of matrix rows equals the vector length, 
		then the plot function plots each matrix column versus the vector. 
	If the number of matrix columns equals the vector length,
		 then the function plots each matrix row versus the vector. 
	If the matrix is square, 
		then the function plots each column versus the vector	
If one of X or Y is a scalar and the other is either a scalar or a vector,
	 then the plot function plots discrete points. 
	 However, to see the points you must specify a marker symbol, for example, plot(X,Y,'o').

Exemples:

  1. X ,Y 都是向量
>> X = [1,4,10];
>> Y = [13,28,10]; % X, Y 都是向量
>> plot(X,Y);	% 以X 为横坐标, 对应位置的 Y 为纵坐标 , 画出对应点, 用线段连接.

在这里插入图片描述
X 和 Y 的元素对应.

  1. matrix versus matrix
>> A = [11,12,13;21,22,23;31,32,33]

A =

    11    12    13
    21    22    23
    31    32    33
>> C = [1,2,3;4,5,6;7,8,9]
C =

     1     2     3
     4     5     6
     7     8     9
>> plot(A,C)

在这里插入图片描述
plot画图中矩阵的每一列互相对应

  1. vector versus square matrix
>> A

A =

    11    12    13
    21    22    23
    31    32    33

>> X

X =

     1     4    10

>> plot(A,X);

A 的每一列对应X 中相应元素
在这里插入图片描述

>> plot(X,A);

在这里插入图片描述
X 的对应元素 对应A 的每一列.

plot(Y)

官方文档
plot(Y) creates a 2-D line plot of the data in Y versus the index of each value.

If Y is a vector, then the x-axis scale ranges from 1 to length(Y).	
If Y is a matrix, then the plot function plots the columns of Y versus their row number. 
	The x-axis scale ranges from 1 to the number of rows in Y.	
If Y is complex, then the plot function plots the imaginary part of Y versus the real part of Y,
	 such that plot(Y) is equivalent to plot(real(Y),imag(Y)).

max

返回数组的最大元素.
max(A) 如果A 是一个矩阵, 就返回一个行向量,其中每个元素对应矩阵每一列的最大值

power 和 mpower

函数语法
power( 矩阵点幂) C = A.^B 等价于 C = power(A,B)
mpower( 矩阵幂运算) C = A^B 等价于 C = mpower(A,B)

暂时写这么多,后续继续补充,欢迎关注

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值