基本操作
-
PS1('>> ')
:簡(jiǎn)化命令行
-
%
:注釋,~=
:不等于,&&
:且阿蝶,||
:或
-
a = 3
, a = 3;
:后者抑制屏幕顯示
-
disp()
:打印
-
disp(sprintf('hello world %.2f' , pi))
:格式化字符串
-
format long
, format short
:控制輸出長(zhǎng)度
向量和矩陣
1. v = [1 2 3], v = [1; 2; 3]
2. v = 1:6, v = 1:0.1:2
3. A = [1 2; 3 4; 5 6]
4. ones(2,3), 2 * ones(2,3), zeros(1,3)
5. rand(3,3), randn(3,3)
6. hist(randn(1,10000)), hist(randn(1,10000),50)
7. eye(3)
8. help rand, help help
加載存儲(chǔ)數(shù)據(jù)
1.size(A), size(A,1), size(A,2)
2.pwd
3.length()
4.cd, ls
5.load
6.who, whos:顯示變量
7.clear, clc
8.save hello.mat v, save hello.txt v
9.A(3,2), A(2,:), A(:,2):索引方式
10.A = [A,[100;101;102]]
11.A(:):將矩陣用列向量列出
12.C = [A B], C = [A;B]
數(shù)據(jù)運(yùn)算
1.A * C, A.* C(元素位運(yùn)算)
2.log(), exp(), abs(), max()
3.A'
4.[val,ind] = max(a):獲取變量值及其索引
5.find(a<3):找出bool為真的索引
6.magic(3):獲取行冰木、列以及對(duì)角全相等的方陣
7.[r,c] = find(A>=7):獲取矩陣的行、列索引
8.sum(), prod(), ceil(), floor()
9.max(A,[],1), max(A,[],2):max()默認(rèn)按列取最大值
10.max(A(:)):取出矩陣所有值的最大值
11.flipud():將矩陣上下交換
12.pinv():返回逆
繪圖
1.plot(x,y)
2.xlabel(), ylabel(), legend(), title()
3.print -dpng 'name.png'
4.close, close all
5.figure() :打開(kāi)新窗口繪圖
6.subplot(1,2,1)
7.axis([0.5 1 -1 1]):設(shè)置橫縱軸范圍
8.clf:清除
9.imagesc(A), colorbar, colormap gray:可視化矩陣
控制命令
for i = 1:10,
v(i) = 2^i;
end;
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;
v(1) = 2;
if v(1) == 1,
disp('this is one');
elseif v(1) == 2,
disp('this is two');
else
disp('not one or two');
end;
函數(shù)
function y = costFunction(x)
y = x ^ 2;
function [y1,y2] = costFunction(x)
y1 = x ^ 2;
y2 = x ^ 3;
1.addpath():添加環(huán)境變量
2.exit
理解向量化
- 線性代數(shù)夸浅、矩陣的運(yùn)用,提高運(yùn)算速度