matlab與機器學習(二)程序基本操作(含代碼解析)

備註:%%之間的爲註釋內容%%,%註釋後面的內容

            matlab形成自己的編程風格更有利於簡潔的編程界面

            圖像的初步處理可應用於論文撰寫上,文章更有說服力

            程序調試有利於發現邏輯問題,都是必備的基礎知識

%% I. 清空環境變量及命令

% clear all
clc

一、MATLAB編程習慣與風格

%% II. MATLAB編程習慣與風格

x_coordinate = rand(1,10);
y_coordinate = rand(1,10);
figure
plot(x_coordinate,y_coordinate,'r-*')

二、程序調試

%% III. MATLAB程序調試

% %%
% % 1. Index must be a positive integer or logical.
% A = [1 2 3 4 5];
% A(0)
% A(3.5)
% A(-2)
%
% %%
% % 2. Undefined function or variable 'B'.
% B
%
% %%
% % 3. Inner matrix dimensions must agree.
% B = [1 2 3];
% A * B
%
% %%
% % 4. Function definitions are not permitted at the prompt or in scripts.
% function c = add(a,b)
% c = a + b;
%
% %%
% % 5. Index out of bounds because numel(A)=5.
% A(6)
%
% %%
% % 6. In an assignment  A(I) = B, the number of elements in B and I must be the same.
% A(3) = B;
%
% %%
% % 7. Expression or statement is incorrect--possibly unbalanced (, {, or [.
% mean(A(1:3)
%
% %%
% % 8. Too many input arguments.
% mean(A,1,2)

%%
% 9. 循環體的調試
a = 1:100;
b = [];
for i = 21:21
    index = 105 - 5*i;
    b = [b a(index)];
end

%%
% 10. 查看、編輯MATLAB自帶的工具箱函數
edit mean

edit newff

三、內存優化

%% III. MATLAB內存優化配置

feature memstats
%
%% IV. 向量化編程
%%
% 1. 及時清除不用的變量
a = rand(10000);
b = rand(10000);
clear a
b = rand(10000);

%%
% 2. 使用變量前,預分配內存空間
clear all
clc
n = 30000;
tic;
for k = 1:n
    a(k) = 1;
end
time = toc;
disp(['未預分配內存下動態賦值長爲',num2str(n),'的數組時間是:',num2str(time),'秒!'])

tic
b = zeros(1,n);
for k = 1:n
    b(k) = 1;
end
time = toc;
disp(['預分配內存下動態賦值長爲',num2str(n),'的數組時間是:',num2str(time),'秒!'])

%%c
% 3. 選擇恰當的數據類型
clear all
clc
n = 300000;
a = 8;
b{1} = 8;
c.data = 8;

tic
for k = 1:n;
    a;
end
time = toc;
disp(['訪問',num2str(n),'次double型數組時間是:',num2str(time),'秒!'])

tic
for k = 1:n;
    b{1};
end
time = toc;
disp(['訪問',num2str(n),'次cell型數組時間是:',num2str(time),'秒!'])

tic
for k = 1:n;
    c.data;
end
time = toc;
disp(['訪問',num2str(n),'次struct型數組時間是:',num2str(time),'秒!'])

%%
% 4. 按列優先循環
clear all
clc
n = 1000;
a = rand(n);
tic
for i = 1:n
    for j = 1:n
        a(i,j);
    end
end
toc

for i = 1:n
    for j = 1:n
        a(j,i);
    end
end
toc

%%
% 5. 循環次數多的變量安排在內層
clear all
clc
tic
a = 0;
for i = 1:1000
    for j = 50000
        a = a + 1;
    end
end
toc

tic
a = 0;
for i = 1:50000
    for j = 1:1000
        a = a + 1;
    end
end
toc

%%
% 6. 給一些函數“瘦身”
edit mean
clear all
clc
a = rand(1,10000);
tic
b = mean(a)
toc

tic
c = sum(a)/length(a)
toc

四、圖像對象和句柄

%% V. 圖像對象和句柄

%%
% 1. 如何設置線條的屬性呢?
x = 0:0.01:2*pi;
y = sin(x);
h = plot(x,y);
grid on
get(h)
set(h,'linestyle','-','linewidth',2,'color','k')

%%
% 2. 如何修改網格的間隔呢?  
set(gca,'xtick',0:0.5:7)
set(gca,'ytick',-1:0.1:1)

%%
% 3. 如何設置圖例的字體及大小呢?
x = 0:0.01:2*pi;
y1 = sin(x);
y2 = cos(x);
plot(x,y1,'r')
hold on
plot(x,y2,'-.b')
h = legend('sin(x)','cos(x)');
set(h,'fontsize',16,'color','k','edgecolor','r','textcolor','w')

%%
% 4. 如何拆分圖例呢?
x = 0:0.01:2*pi;
y1 = sin(x);
y2 = cos(x);
h1 = plot(x,y1,'r');
hold on
h2 = plot(x,y2,'-.b');
ax1 = axes('position',get(gca,'position'),'visible','off');
legend(ax1,h1,'sin(x)','location','northwest')
ax2 = axes('position',get(gca,'position'),'visible','off');
legend(ax2,h2,'cos(x)','location','northeast')

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章