zoukankan      html  css  js  c++  java
  • Matlab 常用函数小结

    表格导入/导出:

    csvread

    Matrix = csvread('filename') //读取表格文件data.csv到矩阵Matrix
    Matrix = csvread('filename', row, col)//读取文件filename中的数据,起始行为row,起始列为col,需要注意的是,此时的行列从0开始。
    Matrix = csvread('filename', row, col, range)//读取文件filename 中的数据,起始行为 row,起始列为col,读取的数据由数组 range 指定,range 的格式为:[R1 C1 R2 C2],其中R1、C1为读取区域左上角的行和列,R2、C2为读取区域右下角的行和列。

    csvwrite

    csvwrite('filename',Matrix) //将数组Matrix中的数据保存为文件filename,数据间以逗号分隔。
    csvwrite('filename',Matrix,row,col) //将数组Matrix中的指定数据保存在文件中,数据由参数 row和col指定,保存row和col右下角的数据。

    xlsread

    Matrix = xlsread('filename') //读取文件filename中的数据,返回到Matrix中。
    Matrix = xlsread(filename, sheet) //选择所在的sheet,比如'sheet1'。
    Matrix = xlsread(filename, range) //所在的单元格范围,比如range='A1:A8'。
    Matrix = xlsread(filename, sheet, range) //sheet和单元格范围同时限制。

    xlswrite

    xlswrite(filename, Matrix) //将矩阵Matrix的数据写入名为filename的Excel文件中。
    xlswrite(filename, Matrix, sheet) //将矩阵Matrix的数据写入文件名为filename中的指定的sheet中。
    xlswrite(filename, Matrix, range) //将矩阵Matrix中的数据写入文件名为filename的Excel文件中,且由range制定存储的区域,例如'A1:A2'。
    xlswrite(filename, Matrix, sheet, range) //在上一条命令的基础上指定了所要存储的sheet。

    基本矩阵:

    eye

    Matrix = eye(n) //返回n*n单位矩阵;
    Matrix = eye(m,n) //返回m*n单位矩阵;
    Matrix = eye([m n]) //返回m*n单位矩阵;
    Matrix = eye //标量1

    ones

    Matrix = ones(a,b) //返回a行b列全1数组
    Matrix = ones(a) //返回a行a列全1数组

    zeros

    Matrix = zeros(n) //返回n行n列全零矩阵。
    Matrix = zeros(m,n) //返回m行n列全零矩阵。
    Matrix = zeros //标量0。

    linspace

    Matrix = linspace(x1,x2,n) //返回从x1到x2均匀分布的n点行向量。

    logspace

    Matrix = logspace(x1,x2,n) //返回从x1到x2n点行向量,元素分布形成一个对数曲线。

    rand

    Matrix = rand(n) //返回一个n x n的随机矩阵。
    Matrix = rand(m,n)  or  Matrix = rand([m n])  //返回一个m x n的随机矩阵。 
    Matrix = rand(m,n,p,...)  or  Matrix = rand([m n p...]) //产生随机数组。 
    Matrix = rand(size(A)) //返回一个和A有相同尺寸的随机矩阵。

    矩阵信息:

    disp

    disp('Hello world') //打印字符串
    disp(Matrix) //打印矩阵信息

    isempty

    bool = isempty(Matrix) //矩阵为空,返回1,非空,返回0

    length

    L = length(Matrix) // 返回矩阵长度,比较行数列数,取较大值。

    size

    [row,col]= size(Matrix) //返回矩阵的行数和列数

    find

    ind = find(X) //找出矩阵X中的所有非零元素,并将这些元素的线性索引值(按列)返回到向量ind中。  
    ind = find(X, k) or ind = find(X, k, 'first')  //返回第一个非零元素k的索引值。
    ind = find(X, k, 'last'//返回最后一个非零元素k的索引值。

    字符串:

    strcat

    Example:
     
            strcat({'Red','Yellow'},{'Green','Blue'})
     
    returns
     
            'RedGreen'    'YellowBlue'

    strcmp

    strcmp(S1,S2) //判断两个字符串是否相同,若是,就返回True,否则返回False

    sprintf

    sprintf(S) //创建含有格式控制的字符串
    sprintf('%0.5g',(1+sqrt(5))/2)       // 1.618
    sprintf('%0.5g',1/eps)               // 4.5036e+15       
    sprintf('%15.5f',1/eps)              // 4503599627370496.00000
    sprintf('%d',round(pi))              // 3
    sprintf('%s','hello')                // hello
    sprintf('The array is %dx%d.',2,3)   // The array is 2x3.

    文件操作:

    fopen

    fid= fopen(filename,permission)
    
    /*******
    fid=+N(N是正整数):表示文件打开成功,文件代号是N.  
    fid=-1 : 表示文件打开不成功。  
    fid在此次文件关闭前总是有效的。
    
    打开方式参数由以下字符串确定:
    r 读出  
    w 写入(文件若不存在,自动创建)  
    a 后续写入(文件若不存在,自动创建)  
    r+ 读出和写入(文件应已存在)  
    w+ 重新刷新写入,(文件若不存在,自动创建)  
    a+ 后续写入(文件若不存在,自动创建))  
    w 重新写入,但不自动刷新  
    a 后续写入,但不自动刷新
    *******/

    fclose

     ST = fclose(FID)    //返回0文件成功关闭
                                 //返回-1失败
    ST = fclose('all')    //关闭所有文件

    fwrite

    count=fwrite(fid,a,precision) 
    /*****
    将矩阵a 写入fid指向的打开的文件,matlab 自动将a元值转换成precision规定的精度,写成列的形式。count是返回值,写入成功,返回写入数据个数,否则返回0。 
    *****/
    
    count=fwrite(fid,a,precision,skip) 
    /*****
    skip:取正整数n , 
    将矩阵a 以步长n 跳选数值写入fid指向的打开的文件,例如,a=1,2,3,4,5,6,7,8,9 skip=2; 则输入 _,_,3,_,_,6,_,_,9。这对非连续数据场。 
    *****/

    fread

    [a,count]=fread(fid,size,precision)
    [a,count]=fread(fid,size,precision,skip)
    
    //用法对应于fwrite()

    图片处理:

    imread

    A=imread(filename,fmt)
    [X,map]=imread(filename,fmt)
    //从图像文件中读取(载入)图像

    imwrite

    imwrite(A,filename,fmt)
    imwrite(X,map,filename,fmt)
    //把图像写入(保存)图像文件中

    imcrop

    I2=imcrop(I)
    X2=imcrop(X,map)
    RGB2=imcrop(RGB)
    I2=imcrop(I,rect)
    X2=imcrop(RGB,rect)
    //剪切图像

    imshow

    //显示图片文件
    imshow('board.tif')    
    
    //显示内部图片
    [X,map] = imread('trees.tif');         
    imshow(X,map)    
    
    //显示灰度图,并调整显示范围         
    h = imshow(I,[0 80]);

    绘图:

    plot

    plot(x)   //当x 为一向量时,以x 元素的值为纵坐标,x 的序号为横坐标值绘制 曲线。
                //当x 为一实矩阵时,则以其序号为横坐标,按列绘制每列元素值相对于其序号的曲 线。 
    plot(x,y)  //以x 元素为横坐标值,y 元素为纵坐标值绘制曲线。 
    plot(x,y1,x,y2,…) //以公共的x 元素为横坐标值,以y1,y2,… 元素为纵坐标值绘制曲线。       

    plot3

    plot3(x,y,z)     //当x,y,z为向量时,绘制一条空间线
    plot3(X,Y,Z)    //当X,Y,Z为行列相同的矩阵式,绘制n条空间线
     plot3(x1,y1,z1,s1,x2,y2,z2,s2,x3,y3,z3,s3,...) //绘制对应数量的空间线

    mesh

    mesh(z)    //以z矩阵的元素值作为z轴自变量,而以矩阵的列、行下标作为x,y轴自变量,画三维网线图
    mesh(x,y,z)    //以x,y,z为三轴自变量,画三维网线图
    mesh(x,y,z,c)    //指定网线颜色为c

    surf

    surf(z)   // 以z矩阵的元素值作为z轴自变量,而以矩阵的列、行标作为x,y轴自变量,画三维曲面图
    surf(x,y,z)    //以x,y,z为三轴自变量,画三维曲面图
    surf(x,y,z,c)    //指定曲面颜色为c

    axis

    axis([xmin xmax ymin ymax]) //设置坐标轴的最小最大值

    grid

    grid on (gird off)  //给当前图形标记添加(取消)网络

    view

    view(az,el)
    /*****
    az是azimuth(方位角)的缩写,EL是elevation(仰角)的缩写。
    它们均以度为单位。系统缺省的视点定义为方位角-37.5°,仰角30°。
    当x轴平行观察者身体,y轴垂直于观察者身体时,az=0; 
    以此点为起点,绕着z轴顺时针运动,az为正,逆时针为负。
    *****/
  • 相关阅读:
    python函数定义,函数参数
    jmeter之实战总结
    Codeforces Round #384 (Div. 2) B. Chloe and the sequence
    Codeforces Round #384 (Div. 2) C. Vladik and fractions
    CodeForces
    Codeforces Round #383 (Div. 2) B. Arpa’s obvious problem and Mehrdad’s terrible solution
    Codeforces Round #385 (Div. 2) A. Hongcow Learns the Cyclic Shift
    CodeForces
    CodeForces
    Codeforces Round #382 (Div. 2) B. Urbanization
  • 原文地址:https://www.cnblogs.com/moranBlogs/p/3947611.html
Copyright © 2011-2022 走看看