矩阵函数机器学习-Matlab 编程常用命令速览(Ng-ML-class Octave/Matlab Tutorial)

最近一直在查找矩阵函数之类的问题,上午正好有机会和大家分享一下.

    机器学习- Matlab 编程常用命令速览

    --总结自Ng-ML-class Octave/Matlab Tutorial Coursera

    

A、Basic operations and Moving data around

    

1 在命令行模式用shift + 回车便可附加下一行输出

    

2 length命令apply到矩阵时返回较高的一维的dimension

    

3 help + 命令是显示命令的简要帮助信息

    

   doc + 命令 是显示命令的详细帮助文档

    

4 who 命令 显示 以后全部创立的变量

    

   whos 命令 显示以后全部创立变量的详细信息

    

5 保存变量到.mat 文件save hello.mat b 以二进制压缩保存数据

    

   save hello.txt v -ascii 以可读形式文件保存 即文本格式

    

6  :means every elements in this col

    

7 A([1 3], :) 获取第 1、3两行全部列的数据

    

8 A = [A, [100; 101; 102]] 在A矩阵后面加一列col vector [100,101,102]

    

9 size(A) 返回一个1行2列矩阵  表明第1和第2个dimensional 的巨细

    

10 C = [A B]等价于C = [A, B] []为向后面的列添加,连接两个矩阵 [] 为concat 连接矩阵或者字符串

    

11 C= [A; B] ;号表现向上面行添加,因此会增长响应行数,列数不变

    


    

B、Computing on data

    

12  A.*B是矩阵/向量点乘  A*B是矩阵相乘

    

13 log(v) 和exp(v)求以e为底的对数和指数

    

14 abs()求绝对值

    

15 A‘ 求A的转置矩阵

    

16 max函数返回矩阵中最大元素的值和索引   [val, ind] = max(a)

    

17 A < 3 会判断A当中的每个是不是小于3,若小于3,对应位置返回true,否则对于位置返回false

    

18 find(A<3) 返回矩阵中全部值小于3的索引

    

19 magic  Magic square.

    magic(N) is an N-by-N matrix constructed from the integers

    1 through N^2 with equal row, column, and diagonal sums.

    Produces valid magic squares for all N > 0 except N = 2.

    

20  [r, c] = find(A >= 7) 返回值大于等于7的element的row及col的索引

    

21 prod(a) 求矩阵a里面全部元素的乘积

    

    floor(a) 对矩阵a中元素向下取整

    

    ceil(a)对矩阵a中元素向上取整

    

    rand(3) 生成3X3的随机方阵

    

    max(A,[],1) 求矩阵A的每一列的最大值(最后一维是1表明为dimension 1)

    

    max(A,[],2) 求矩阵A的每一行的最大值

    

    sum(A, 1) 对矩阵A第一维度(即每列)求和(注意matlab中第一维默许是列,然后是行,再然后依次类推。。。)

    

    sum(A, 2) 对矩阵A第二维度(即每行)求和

    

    sum(sum(A.*eye(9)))  求矩阵A的对角线元素之和

    

22 矩阵翻转操纵

    

   flipud  Flip matrix in up/down direction. 将矩阵上下翻转, 

    

    类似还有阁下翻转 fliplr, rot90, flipdim.
    flipud(X) returns X with columns preserved and rows flipped
    in the up/down direction.  For example,
    X = 1 4      becomes  3 6
        2 5                       2 5
        3 6                       1 4

    

23 pinv(A) 及inv(A) 求矩阵A 的逆矩阵

    

  

    

C、Plotting data

    

24 t = [0.1 : 0.01 : 0.98]

    

    y = sin(t)

    

    plot(t, y) 画正弦曲线

    

25 hold on; 保存以后曲线,画下一条曲线

    

26 xlabel 标定x轴说明

    

27 legend('sin','cos') 添加图例

    

28 title('my plot') 添加图片标题

    

29  print -dpng 'myPlot.png' 保存图片

    

30 线条颜色标注控制

    

   b     blue          .     point              -     solid
   g     green         o     circle             :     dotted
    r     red           x     x-mark             -.    dashdot 
    c     cyan          +     plus               --    dashed   
    m     magenta       *     star             (none)  no line
     y     yellow        s     square
     k     black         d     diamond
     w     white         v     triangle (down)
                               ^     triangle (up)
                               <     triangle (left)
                               >     triangle (right)
                               p     pentagram
                               h     hexagram

    

31 subplot 画子图,即将多个图合成一个图

    

>> subplot(1,2,1) % Divides plot a 1X2 grid, access the 1st element
>> plot(t,y1)
>> subplot(1,2,2)
>> plot(t,y2)

    

32 grid 加上网格

    

33 figure(1)

    >> plot(t, y1)

    >> figure(2)

    >> plot(t,y2)

    

将y1与y2的曲线画到文件名为figure1和figure2的两个文件中

    

34 axis([0.5 1 -1 1]) 设定x轴范围为0.5~1,y轴范围为-1~1

    

35 clf Clear current figure.

    

36  imagesc(A)

    

imagesc Scale data and display as image.

    

把矩阵A画成黑色小方格

    

37  imagesc(A), colorbar, colormap gray;

    

colorbar 显示颜色渐变条,表明颜色含义

    

colormap 设置colormap性子  即RGB三色

    

 A color map matrix may have any number of rows, but it must have
    exactly 3 columns.  Each row is interpreted as a color, with the
    first element specifying the intensity of red light, the second
    green, and the third blue.  Color intensity can be specified on the
    interval 0.0 to 1.0.

    

38  a = 1; b = 2; c=3; 不会打出a b c的值

    

      a = 1, b = 2, c=3  会打出a b c 的值

    


    


    

D、Control statements: for, while, if statements

    

39 for 循环

    

 for i = 1:10,
     v(i) = 2 ^i;
   end;
>> v

v =

           2
           4
           8
          16
          32
          64
         128
         256
         512
        1024

    

>> indices = 1:10
 for i = indices,
      disp(i)
   end;

    


    

40 while 循环

    

>> while i <= 5,
     v(i) = 100;
     i = i+1;
   end;
>> v

v =

         100
         100
         100
         100
         100
          64
         128
         256
         512
        1024

>> i = 1;
>>  while true,
     v(i) = 999;
     i = i + 1;
     if i == 6,
         break;
      end;
     end;
>> exit

    



v =

         999
         999
         999
         999
         999
          64
         128
         256
         512
        1024

    

41 if elseif 判断分支语句

    

 v(1)=2

v =

           2
         999
         999
         999
         999
          64
         128
         256
         512
        1024

>> if v(1) == 1,
     disp('The value is one');
    elseif v(1) == 2,
     disp('The value is two');
    else
     disp('The value is not one or two.');
     end;
The value is two

    


    

42  函数的定义和应用

    

定义函数   squareThisNumber.m文件

    

function  y = squareThisNumber(x)

    

 
    每日一道理
闷热的天,蝉儿耐不住寂寞地不停在鸣叫,我孤单一人,寂静的身旁没有一个知音,想疯狂地听摇滚乐,听歇斯底里的歌声,那只为逃避无人的世界里那浓烈的孤单气息。一个人是清冷,两个人便是精彩,于是,莫名的冲动让我格外想念旧日的好友,怀念过去的日子,尽管不够现实的遐想追回不了曾经一切,但却希望思绪可以飞扬于闭上双目后的世界中,印有微笑,印有舞动的身姿,翩翩起舞……

    

y = x^2;

    

调用

    

 squareThisNumber(5)

ans =

    25

    

43 addpath 增长matlab搜索函数的路径

    

44 matlab里面定义的函数可以返回多于一个值, 这是其与C C++等编程语言

    

    的不同之处,C\C++里面的函数有独一返回值

    

    可以答应多个返回值可以带来编程上的方便

    

    function  [a,b] = squareAndCubeThisNumber(x)

    

 

    

a = x^2;

    

b = x^3;

    

调用

    

>> [x1,x2] = squareAndCubeThisNumber(5)

x1 =

    25


x2 =

   125

    


    

45 cost function J 函数示例

    


    

function  J = costFunctionJ(X, y, theta)
 
% X is the "design matrix" containing our training examples.
% Y is the class labels
 
m  = size(X,1);  % number of training examples
predictions = X*theta;  % predictions of hypothesis on all m examples
sqrErrors = (predictions - y).^2;  % squared errors
J = 1/(2*m) * sum(sqrErrors);

调用
X = [1 1; 1 2; 1 3];
y = [1; 2; 3];
theta = [0;1];

>> j = costFunctionJ(X, y, theta)

j =

     0

    

% squared errors is 0 in this example
>> theta = [0;0]

theta =

     0
     0

>> j = costFunctionJ(X, y, theta)

j =

    2.3333
% squared errors is 2.3333  in this example
% which is (1^2 + 2^2 + 3^2) / (2 * 3) = 2.3333

E、Vectorization
Matlab中“向量化”的实现算法,可以减少不必要的循环,对向量全部值的计算
操纵一致,这是更倏地更高效的算法实现思路,要注意体会matlab中“向量化”
“矩阵化”操纵变量实现算法与其他编程语言Java , C, C++的不同
矩阵和函数



文章结束给大家分享下程序员的一些笑话语录: 大家喝的是啤酒,这时你入座了。
你给自己倒了杯可乐,这叫低配置。
你给自已倒了杯啤酒,这叫标准配置。
你给自己倒了杯茶水,这茶的颜色还跟啤酒一样,这叫木马。
你给自己倒了杯可乐,还滴了几滴醋,不仅颜色跟啤酒一样,而且不冒热气还有泡泡,这叫超级木马。
你的同事给你倒了杯白酒,这叫推荐配置。
菜过三巡,你就不跟他们客气了。
你向对面的人敬酒,这叫p2p。
你向对面的人敬酒,他回敬你,你又再敬他……,这叫tcp。
你向一桌人挨个敬酒,这叫令牌环。
你说只要是兄弟就干了这杯,这叫广播。
有一个人过来向这桌敬酒,你说不行你先过了我这关,这叫防火墙。
你的小弟们过来敬你酒,这叫一对多。
你是boss,所有人过来敬你酒,这叫服务器。
酒是一样的,可是喝酒的人是不同的。
你越喝脸越红,这叫频繁分配释放资源。
你越喝脸越白,这叫资源不释放。
你已经醉了,却说我还能喝,叫做资源额度不足。
你明明能喝,却说我已经醉了,叫做资源保留。
喝酒喝到最后的结果都一样
你突然跑向厕所,这叫捕获异常。
你在厕所吐了,反而觉得状态不错,这叫清空内存。
你在台面上吐了,觉得很惭愧,这叫程序异常。
你在boss面前吐了,觉得很害怕,这叫系统崩溃。
你吐到了boss身上,只能索性晕倒了,这叫硬件休克。

--------------------------------- 原创文章 By
矩阵和函数
---------------------------------

posted @ 2013-05-27 19:54  xinyuyuanm  阅读(370)  评论(0编辑  收藏  举报