# Matlab notes
# 匯檔
```matlab
present01=importdata('practice_data_1.txt');
```
# plot
```matlab
%單一函數圖
t=0:0.1:2*pi;
x=2*t;
y=t.*sin(t).*sin(t);
plot(x,y)
%多個函數圖
x=linspace(0,2*pi,100); % x-scale為(0到2*pi拆出100等分)
plot(x,sin(x),x,2*sin(x),x,3*sin(x))
%畫出不同線型與顏色
x=(0:pi/100:2*pi)'; % ':轉制成n*1 matrix
class(x) % check variable type
y1=2*exp(-0.5*x)*[1,-1];
y2=2*exp(-0.5*x).*sin(2*pi*x); % .* : 按元素乘法
x1=(0:12)/2;
y3=2*exp(-0.5*x1).*sin(2*pi*x1);
plot(x,y1,'k:',x,y2,'b--',x1,y3,'rp');
%圖形輔助
x=0:pi/50:2*pi;
y1=sin(x);
y2=cos(x);
figure
plot(x,y1,'k-',x,y2,'k-.');
%text標註
text(pi,0.05,'\leftarrow sin(\alpha)');
text(pi/4-0.05,0.05,'cos(\alpha) \rightarrow ');
%title標註
title('sin(\alpha) and cos(\alpha)')
%標座標
xlabel('\alpha')
ylabel('sin(\alpha) and cos(\alpha)')
%畫2*1的兩張圖
subplot(2,1,1)
plot(x,y1,'k-',x,y2,'k-.');
title('sin(\alpha) and cos(\alpha)')
subplot(2,1,2)
plot(x,y2)
axis([0.4 0.6 -1 1]) %圖軸上下界x=[0.4 0.6];y=[-1,1]
title('複雜函數的局部透視')
% pie chart
x=[190 33 45 42 45];
explode=[1 1 0 0 0]; %分離顯示設置
figure()
colormap hsv %條色調用的
pie(x,explode,{'生活費','資料費','電話費','購買衣服','其他費用'})
title('pie chart')
% histogram
% 隨機生5*3的data
Y=round(rand(5,3)*10); %round:四捨五入
subplot(2,2,1)
bar(Y,'group')
title('Group')
%堆型二維垂直條形圖
subplot(2,2,2)
bar(Y,'stack')
title('stack')
%堆型二維水平條形圖
subplot(2,2,3)
barh(Y,'stack')
title('stack')
%設定條形寬度1.5
subplot(2,2,4)
bar(Y,1.5)
title('width=1.5')
%barplot
y=[190 33 45 42 45];
x=1:5;
figure
bar(x,y)
title('柱狀圖')
set(gca,'xTicklabel',{'生活費','資料費','電話費','購買衣物', '其他'}) %讓字串放到x軸
%排列圖 (一個橫軸,兩個縱軸其中,横坐 标表示各因素,
% 左纵坐标表示频数,右纵 坐标表示频率,折线表示累积的频率。)
% 该图能较好地分析各因素的重要性,可用于寻找主要问题或主要原因。
y=[100 98 97 90 90];
names={'第一' '第二' '第三' '第四' '第五'};
pareto(y,names)
% 3D plot
t=0:pi/50:2*pi;
x=8*cos(t);
y=4*sqrt(2)*sin(t);
z=-4*sqrt(2)*sin(t);
plot3(x,y,z,'p');
title('Line in 3D space');
text(0,0,0,'origin');
xlabel('X');ylabel('Y');zlabel('Z');grid;
%surface plot
% surf(X, Y, Z):绘制三维表面图,X、Y、Z 分别表示三维网格图形在 x 轴、y 轴和 z 轴的坐标,图形的颜色由矩阵 Z 决定。
% surf(X, Y, Z, C):绘制三维表面图,输入参数 C 用于控制绘制的三维表面图的颜色。
% surf(..., 'PropertyName', PropertyValue):绘制三维表面图,设置相应属性的属性值。
% 函数 surfc()用于绘制带等值线的三维表面图,其调用格式同函数 surf()基本相同,函数 surfl()可用于绘制带光照模式的三维表面图,与函数 surf()和 surfc()不同的调用格式如下:
% surfl(...,'light'):以光照对象 light 生成一个带颜色、带光照的曲面。surfl(...,'cdata'):输入参数 cdata 设置曲面颜色数据,使曲面成为可反光的曲面。
% surfl(...,s):输入参数 s 为一个二维向量[azimuth,elevation],或者三维向量[x,y,z],用于指定光源方向,默认情况下光源方位从当前视角开始,逆时针 45°。
xi=-10:0.5:10;
yi=-10:0.5:10;
[x,y]=meshgrid(xi,yi);
z=sin(sqrt(x.^2+y.^2))./sqrt(x.^2+y.^2);
surf(x,y,z)
```
# 常用資料處理
```matlab
A=rand(3,2)
A=[1 2; 3 4;5 6]
A(3,2)
A(2,:) %call 第二列的列向量
A([1 3],:) %select the 1st and 3rd row
save test.mat A
A(:,2)=[10,11,12]
A=[A,[101;102;103]]%append another column vector
A(:)%put all elements of A into a single vector 也就是把矩陣拉直
%資料運算
B=[11,12;13,14;15,16]
C=[A B]
A=[1 2;3 4;5 6]
C=[A;B]
A.*B%對應位置的資料相乘,即element product
A'%轉置
A<3
find(A<3)
[r,c]=find(A>=6) %r:列 c:行
a=[1 15 2 0.5]
sum(a)
prod(a) %所有元素相乘
floor(a)%取下界
ceil(a)%取上界
rand(3)%建立3*3的random矩陣,每個值在[0,1]之間
max(rand(3),rand(3)) %在兩個random的3*3矩陣中找對應位置的max
A=magic(3) %返回由 1 到 n^2 的整数构成并且总行数和总列数相等的 n×n 矩阵。
max(A,[],1) %找每行最大值,1表示第一維,即行(?)
max(A,[],2) %找每列最大值,2表示第二維,即列(?)
max(A) %defaultis column max
max(max(2))
A(:) %
max(A(:))
eye(9) % 9*9 的單位矩陣
A=magic(3);
temp=pinv(A) %矩陣求逆
temp*A
%Plotting Data
t=[0:0.01:0.98];
y1=sin(2*pi*4*t);
plot(t,y1)
hold on; %plot new figure on the old ones
y2=cos(2*pi*4*t);
plot(t,y2,'r')
xlabel('time')
ylabel('value')
legend('sin','cos')
title('my plot')
% for if while statements
v=zeros(10,1)
for i=1:10
v(i)=2^i;
end
v
```