Octave基础

1.     基本运算:
^: 乘方
~=:不等于
xor(n, m): 异或

disp(a): 显示值
printf,sprintf:同C语言

pwd  cd  ls:同linux的目录操作( Octave 是GNU的,因此很多linux命令均支持)

help command

2.     变量和数据类型:
类似python,是动态类型的。

矩阵的表示:[ 1  2 ; 3  4 ;  5  6 ]  用“;”来作为行间的分隔。
vector的表示:[ 1 ; 2 ; 3 ]  列向量
              [1  2  3]  行向量
生成矩阵的方法: ones(2, 3)   same as C = [2 2 2; 2 2 2] 
                 zeros(2, 3)
                    rand(1,3)  % drawn from a uniform distribution (0 - 1)
randn(1,3)  % drawn from a normal distribution (mean=0, var=1)
%上面四个函数可以只使用一个参数,则为方阵
eye(4): 4x4 identity matri
生成行向量的特殊方法:v = [1:0.1:2]  from 1 to 2, with stepsize of 0.1. Useful for plot axes
                      v = [1:6]  from 1 to 6, assumes stepsize of 1

3.     加载和保存数据:
%% dimensions
sz = size(A)  %得到一个二维行向量
size(A,1)  % number of rows
size(A,2)  % number of cols
length(v)  % size of longest dimension,一般只用于vector

%%save data
save hello v;   % save variable v into file hello 除了数据,文件中还保存了变量名字等信息
save hello.txt v -ascii;  %只保存数据,不保存之前的变量名字等信息

%%load data
load filename;  %如果之前的文件保存有变量名,则将数据load到这个变量中。否则用文件名作为变量名

who    % list variables in workspace
whos   % list variables in workspace (detailed view)

4.     向量和矩阵的indexing:(下标从1开始)
% indexing for vector
a(index)

%% indexing for matrix
A(3,2)  % indexing is (row,col)
A(2,:)  % get the 2nd row.
        % ":" means every element along that dimension
A(:,2)  % get the 2nd col
A([1 3],:)  %get the first and third rows
A(:)   % Select all elements as a column vector

A(:,2) = [10; 11; 12]     % change second column
A = [A, [100; 101; 102]];  % append column vector
A = [A; [100 101 102]];  %append row vector
":"默认表示所有,可以用"a:b"表示从a到b所有,选定可以用"[]"

%%将矩阵组合起来
[A B] 
[A; B]

5.     矩阵运算:
A+B, A-B:矩阵加减
A*B:  矩阵乘法
A’: 矩阵转置
pinv(A): 矩阵的逆

A .* B  % element-wise multiplcation(对应元素相乘,同矩阵+和-)
矩阵+,-,*,/一个数都是element-wised.
A .^ 2
1./v
log(v)  % functions like this operate element-wise on vecs or matrices
exp(v)  % e^4
abs(v)

%% misc useful functions

% max (or min)  语法同
%对于vector
a = [1 15 2 0.5]
val = max(a)
[val,ind] = max(a) %最大值和其下标
%对于matrix
max(A)  %返回一个行向量,每列的最大值  等价于 max(A, [], 1)
max(A, [], 2)  %返回一个列向量,每行的最大值
要求整个矩阵的最大值,可以用max(max(A)), 或者max(A(:))

%sum 和 prod (语法同)
a=[1 2 3]
sum(a);
A = magic(9)
sum(A,1) 按列求和,返回一个行向量
sum(A,2) 按行求和,返回一个列向量

% find
a < 3
find(a < 3)
A = magic(3)
[r,c] = find(A>=7

6.     Plotting:
%% plotting
t = [0:0.01:0.98];
y1 = sin(2*pi*4*t);
plot(t,y1);
y2 = cos(2*pi*4*t);
hold on;  % 用于在同一幅图上画多个曲线。"hold off" to turn off
plot(t,y2,'r');  % plot using red color
xlabel('time');
ylabel('value');
legend('sin','cos');
title('my plot');
print -dpng 'myPlot.png'  % save the plot as a file
close;           % or,  "close all" to close all fig

figure(1); plot(t, y1);  % can specify the figure number
figure(2); plot(t, y2);
%默认只在一个窗口中画,后面的会冲掉前面的。使用figure可以通过画多个(可以不用参数)
subplot(1,2,1);  % Divide plot into 1x2 grid, access 1st element
plot(t,y1);
subplot(1,2,2);  % Divide plot into 1x2 grid, access 2nd element
plot(t,y2);
axis([0.5 1 -1 1]);  % change axis scale

%% display a matrix (or image)
imagesc(magic(15))
imagesc(magic(15)), colorbar, colormap gray

7.     For, while, if statements, and functions.

v = zeros(10,1);
for i=1:10
    v(i) = 2^i;
end
% Can also use "break" and "continue" inside for and while loops to control execution.

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

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

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


% Functions

% Create a file called squareThisNumber.m with the following contents (without the %):
% function r = squareThisNumber(x)
%   r = x * x;
% end

squareThisNumber(5); 
% If function is undefine, use "pwd" to check current directory (path),
% and "cd" to change directories
pwd
cd 'C:\Users\ang\Desktop';
squareThisNumber(5); 

% Octave search path (advanced/optional)
addpath('C:\Users\ang\Desktop');
cd 'C:\'
squareThisNumber(5)

%function 可以返回对个值
% Create a file called squareThisNumber.m with the following contents (without the %):
% function [a, b]= squareAndCubeThisNumber(x)
%   a = x * x;
%   b = x * x * x;
% end
调用:[y1, y2] = squareAndCubeThisNumber(x);

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

打赏
文章很值,打赏犒劳作者一下
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏

jiangwennjust

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者