Octave基础教程

2021-02-07  本文已影响0人  pzle

一、基本操作

数学运算与逻辑运算

数学运算

>> 7 + 8
ans = 15
>> 4 - 3
ans = 1
>> 4 * 3
ans = 12
>> 5 / 2
ans = 2.5000

逻辑运算

>> 1 == 2
ans = 0
>> 1 ~= 2
ans = 1
>> 1 && 0 %AND
ans = 0
>> 1 || 0 %OR
ans = 1

向量与矩阵

矩阵

>> A = [ 1 2; 3 4; 5 6]
A =

   1   2
   3   4
   5   6

向量

>> v = [1; 2; 3]
v =

   1
   2
   3

注:构造过程中用“;”分隔,不要用成“:”

其他构造

1.全1/全0矩阵

>> ones(2,3)
ans =

   1   1   1
   1   1   1

>> zeros(2,3)
ans =

   0   0   0
   0   0   0

2.rand/randn
rand构造全部为随机数的矩阵
randn产生标准正态分布的矩阵

>> rand(2,3)
ans =

   0.633492   0.254423   0.102210
   0.516066   0.065798   0.922173

>> randn(2,3)
ans =

  -0.022244  -0.017311   1.508777
  -0.611087   0.368132  -0.575273

3.横向量[起始:步长:终点]

>> v = [1:2:7]
v =

   1   3   5   7

绘图

hist(A)        %以A绘图

注:绘图前注意文件所处目录,如有中文会报错

二、移动数据

基础操作

>> pwd
ans = D:\xunleixiazai\octave-6.1.0-w64        %显示当前目录

>> cd 'D:\octave'        %切换目录

>> ls        %列出当前目录文件
 Volume in drive D is DATA
 Volume Serial Number is 9CEA-0E60

 Directory of D:\octave

[.]           octave1.dat
[..]
               1 File(s)            263 bytes
               2 Dir(s)  42,478,292,992 bytes
 free
>> who        %当前目录下所有被定义的变量
Variables visible from the current scope:

A    a    ans  v

>> whos        %比“who”中的信息更详细
Variables visible from the current scope:

variables in scope: top scope

   Attr Name        Size
Bytes  Class
   ==== ====        ====
=====  =====
        A           3x2
   48  double
        a           1x1
    8  double
        ans         1x32
   32  char
        v           1x4
   32  double

Total is 43 elements using 120 bytes

>> size(A)        %查看向量/矩阵大小
ans =

   3   2

>> clear A        %清楚A矩阵

>> save hello.mat v      &存储二进制形式

>>save hello.txt v -ascii        %存储文档形式

操作数据

1.读取单个元素
例:A(行,列)
A矩阵中第三行第二列元素

2.读取行/列元素
A(:,2)
返回第二列元素,行同理

注:A(:)是将A中所有元素放入一列中

3.附加矩阵
A = [A, [100; 101; 102]]
在A右边添加一列新的列矩阵

4.矩阵相加
左右排列: C = [A B]
上下排列: C = [A; B]

三、计算数据

矩阵计算

同前面数学运算,但在运算符前注意加“.”
例: 1./A

注意点

1.求矩阵的逆
pinv(v)

2.构建单位矩阵
eye()

四、绘图数据

各项参数

xlabel对x轴命名
legend对每条线命名
title添加名字
print保存图像到本地

可视化图像:
imagesc()


可视化后的图像

五、向量化

两者差异

补充:python中的向量化

import numpy as np
a = np.random.rand(1000000)
b = np.random.rand(1000000)
c = np.dot(a, b)

参考:
1.https://blog.csdn.net/qq_29762941/article/details/80139019
2.https://blog.csdn.net/mogoweb/article/details/80329207

上一篇下一篇

猜你喜欢

热点阅读