2015年3月2日 星期一

15.基本陣列(矩陣)運算函數


>> A=magic(3); B=2*A;
>> det(A)
ans =
  -360
>> expm(A)
ans =
  1.0e+006 *
    1.0898    1.0896    1.0897
    1.0896    1.0897    1.0897
    1.0896    1.0897    1.0897
>> A=magic(2)
A =
     1     3
     4     2
>> inv(A)
ans =
   -0.2000    0.3000
    0.4000   -0.1000
>> B=expm(A)
B =
   63.6830   63.5476
   84.7302   84.8655
>> logm(B)
ans =
    1.0000    3.0000
    4.0000    2.0000
>> pow2(B,A)
ans =
  1.0e+003 *
    0.1274    0.5084
    1.3557    0.3395
>> sqrtm(A)
ans =
   0.9583 + 0.8081i   0.9583 - 0.6061i

   1.2778 - 0.8081i   1.2778 + 0.6061i

14.陣列(矩陣)處理函數

陣列(矩陣)處理函數


命令
功能
[A, B] [A  B]
將相同列數的AB陣列合併成新的橫排陣列,此陣列的列數不變。
[A; B]
將相同行數的AB陣列合併成新的縱排陣列,此陣列的行數不變。
cat(dim, A, B)
dim指定,將AB陣列合併成新的陣列,AB陣列的dim維度必須相同。dim1, 2, ...分別表示row, column, ...
diag(A)
回傳陣列A的主對角元素之行向量
diag(A, n)
回傳陣列A的第n個對角元素之行向量
diag(x)
建構一個以x為內容的對角矩陣,除主對角元素以外均為0
flipdim(A, dim)
將陣列Adim維度翻轉。dim1, 2, ...分別表示row, column, ...
fliplr(A)
將陣列A的元素左右翻轉。
flipud(A)
將陣列A的元素上下翻轉。
repmat(A,[m, n, ..., r])
將陣列A的元素沿第一維度重複m次;沿第二維度重複n次,以此類推。
repmat(A, m, n)
將陣列A的元素沿第一維度重複m次;沿第二維度重複n次。
reshape(A, m, n)
將陣列A的元素重新建立一個大小為m×n的陣列元素各數不可增減
rot90(A)
將陣列A的元素逆時針旋轉90度。
rot90(A, n)
將陣列A的元素逆時針旋轉90×n度。
tril(A)
回傳陣列A的下三角陣列
tril(A, n)
回傳陣列A的第n個下三角陣列
triu(A)
回傳陣列A的上三角陣列
triu(A, n)
回傳陣列A的第n個上三角陣列
wshift(type, x, p)
()向量x之循環位移
type可為1'1''1d''1D'p必須為整數,p>0為向左()循環位移;p<0為向右()循環位移;p=0則維持不變。
wshift(type, A, p)
陣列A之循環位移,type可為2'2''2d''2D'p必須為整數的列向量,p(1)控制行循環位移;p(2)控制列循環位移。p(1)>0為向上循環位移;p(1)<0為向下循環位移;p(1)=0則維持不變;p(2)>0為向左循環位移;p(2)<0為向右循環位移;p(2)=0則維持不變。

>> A=magic(3); B=2*A;
>> C=[A; B]
C =
     8     1     6
     3     5     7
     4     9     2
    16     2    12
     6    10    14
     8    18     4
>> D=[A B]
D =
     8     1     6    16     2    12
     3     5     7     6    10    14
     4     9     2     8    18     4
>> E=cat(2,A,B)
E =
     8     1     6    16     2    12
     3     5     7     6    10    14
     4     9     2     8    18     4
>> diag(E)
ans =
     8
     5
     2
>> x=diag(E)
x =
     8
     5
     2
>> diag(x)
ans =
     8     0     0
     0     5     0
     0     0     2
>> diag(E,-1)
ans =
     3
     9
>> diag(E,3)
ans =
    16
    10
     4
>> flipdim(A,1)
ans =
     4     9     2
     3     5     7
     8     1     6
>> fliplr(A)
ans =
     6     1     8
     7     5     3
     2     9     4
>> flipud(A)
ans =
     4     9     2
     3     5     7
     8     1     6
>> repmat(A,[2, 3, 1])
ans =
     8     1     6     8     1     6     8     1     6
     3     5     7     3     5     7     3     5     7
     4     9     2     4     9     2     4     9     2
     8     1     6     8     1     6     8     1     6
     3     5     7     3     5     7     3     5     7
     4     9     2     4     9     2     4     9     2
>> repmat(A,2,3)
ans =
     8     1     6     8     1     6     8     1     6
     3     5     7     3     5     7     3     5     7
     4     9     2     4     9     2     4     9     2
     8     1     6     8     1     6     8     1     6
     3     5     7     3     5     7     3     5     7
     4     9     2     4     9     2     4     9     2
>> reshape(A, 1, 9)
ans =
     8     3     4     1     5     9     6     7     2
>> reshape(A(3:6), 2, 2)                %增加限制元素條件
ans =
     4     5
     1     9 
>> rot90(A)
ans =
     6     7     2
     1     5     9
     8     3     4
>> rot90(A,3)
ans =
     4     3     8
     9     5     1
     2     7     6
>> tril(A)
ans =
     8     0     0
     3     5     0
     4     9     2
>> tril(A,-1)
ans =
     0     0     0
     3     0     0
     4     9     0
>> triu(A)
ans =
     8     1     6
     0     5     7
     0     0     2
>> triu(A,1)
ans =
     0     1     6
     0     0     7
     0     0     0
>> wshift('1d', x, 1)
ans =
     5
     2
     8
>> wshift('1d', x, -1)
ans =
     2
     8
     5
>> wshift('2d', A, [1 -1])
ans =
     7     3     5
     2     4     9
     6     8     1
>> wshift('2d', A, [-1 0])
ans =
     4     9     2
     8     1     6
     3     5     7

2014年11月20日 星期四

繪圖之水平座標與垂直座標刻度設定

Example:

n = [-100:100];
x = cos(0.1*pi*n);
Hx = stem(n,x,'r--','filled');
set(Hx,'markersize',2);
axis([-110, 110,-2,2]);
xtick = [-100:18:100];
ytick = [-2:0.5:2];
set(gca,'XTickMode','manual','XTick',xtick);
set(gca,'YTickMode','manual','YTick',ytick);
xlabel('n','FontSize',10);
ylabel('x(n)','FontSize',10);
其中水平座標刻度設定

xtick = [刻度由此開始:刻度間隔:刻度結束]

垂直座標刻度設定亦同

ytick = [刻度由此開始:刻度間隔:刻度結束]


2014年9月3日 星期三

更改繪圖axis字型

Example

n = [0:20]; x = [(n-3) == 0];
hd = stem(n,x,'k');
axis([0,20,min(x),max(x)]);
set(get(hd,'Parent'),'FontName','times new roman')


Results

原始圖(字型為Helvetica)

更改後(字型為times new roman)




觀察繪圖區內的屬性

Example:

n = [0:20]; x = [(n-3) == 0];
hd = stem(n,x,'k');
axis([0,20,min(x),max(x)]);
get(get(hd,'Parent'))

Results:
ActivePositionProperty = outerposition
ALim = [0 1]
ALimMode = auto
AmbientLightColor = [1 1 1]
Box = on
CameraPosition = [10 0.5 17.3205]
CameraPositionMode = auto
CameraTarget = [10 0.5 0]
CameraTargetMode = auto
CameraUpVector = [0 1 0]
CameraUpVectorMode = auto
CameraViewAngle = [6.60861]
CameraViewAngleMode = auto
CLim = [0 1]
CLimMode = auto
Color = [1 1 1]
CurrentPoint = [ (2 by 3) double array]
ColorOrder = [ (7 by 3) double array]
DataAspectRatio = [20 1 2]
DataAspectRatioMode = auto
DrawMode = normal
FontAngle = normal
FontName = Helvetica
FontSize = [10]
FontUnits = points
FontWeight = normal
GridLineStyle = :
Layer = bottom
LineStyleOrder = -
LineWidth = [0.5]
MinorGridLineStyle = :
NextPlot = replace
OuterPosition = [ (1 by 4) double array]
PlotBoxAspectRatio = [1 1 1]
PlotBoxAspectRatioMode = auto
Projection = orthographic
Position = [ (1 by 4) double array]
TickLength = [0.01 0.025]
TickDir = in
TickDirMode = auto
TightInset = [ (1 by 4) double array]
Title = [182.015]
Units = normalized
View = [0 90]
XColor = [0 0 0]
XDir = normal
XGrid = off
XLabel = [183.015]
XAxisLocation = bottom
XLim = [0 20]
XLimMode = manual
XMinorGrid = off
XMinorTick = off
XScale = linear
XTick = [ (1 by 11) double array]
XTickLabel = [ (11 by 2) char array]
XTickLabelMode = auto
XTickMode = auto
YColor = [0 0 0]
YDir = normal
YGrid = off
YLabel = [184.015]
YAxisLocation = left
YLim = [0 1]
YLimMode = manual
YMinorGrid = off
YMinorTick = off
YScale = linear
YTick = [ (1 by 11) double array]
YTickLabel = [ (11 by 3) char array]
YTickLabelMode = auto
YTickMode = auto
ZColor = [0 0 0]
ZDir = normal
ZGrid = off
ZLabel = [185.015]
ZLim = [-1 1]
ZLimMode = auto
ZMinorGrid = off
ZMinorTick = off
ZScale = linear
ZTick = [-1 0 1]
ZTickLabel =
ZTickLabelMode = auto
ZTickMode = auto

BeingDeleted = off
ButtonDownFcn =
Children = [ (2 by 1) double array]
Clipping = on
CreateFcn =
DeleteFcn =
BusyAction = queue
HandleVisibility = on
HitTest = on
Interruptible = on
Parent = [1]
Selected = off
SelectionHighlight = on
Tag =
Type = axes
UIContextMenu = []
UserData = []
Visible = on


2014年7月17日 星期四

Legend中的字型與大小也能變更

Example:
>> plot(1:10,[1:10].*cos([1:10]),'-rs',1:10,[1:10].*sin([1:10]),'-.bo',...
1:10,[1:10].*cos([11:20]),'-g*',1:10,[11:20].*sin([11:20]),'-.k+',...
1:10,[1:10].*cos([21:30]),'--c>',1:10,[21:30].*sin([21:30]),'--yd')
>> h = legend('\fontname{標楷體}{隨機選取}\fontname{times new roman}{Lena}',...
'\fontname{標楷體}\fontsize{6}{隨機選取}\fontname{times new roman}\fontsize{10}{F16}',...
'\fontname{標楷體}{隨機選取}\fontname{times new roman}{Pepper}',...
'\fontname{標楷體}{非隨機選取}\fontname{times new roman}{Lena}',...
'\fontname{標楷體}{非隨機選取}\fontname{times new roman}{F16}',...
'\fontname{標楷體}{非隨機選取}\fontname{times new roman}\fontsize{8}{Pepper}');
>> xlabel('\fontname{times new roman}{\itp}')
>> ylabel('\fontname{times new roman}{\itPSNR}')