MATLAB-1:入门基础
生活随笔
收集整理的這篇文章主要介紹了
MATLAB-1:入门基础
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.開發環境介紹
2.變量命名規則
3.數據類型
- 數字
- 字符與字符串
- 矩陣
- 元胞數組
- 結構體
基本操作
%% I. 清空環境變量及命令 clear all % 清除Workspace中的所有變量 clc % 清除Command Window中的所有命令%% II. 變量命令規則 %% % 1. 變量名區分大小寫 A = 2 a = 3%% % 2. 變量名長度不超過63位ABCDEFGHIJKLMNOPQRSTUVWXYZ123456ABCDEFGHIJKLMNOPQRSTUVWXYZ123456 = 3%% % 3. 變量名以字母開頭,可以由字母、數字和下劃線組成,但不能使用標點 % 3A = 4 % .a = 5 % /b = 5 a_2 = 3 % a.2 = 4%% % 4. 變量名應簡潔明了,通過變量名可以直觀看出變量所表示的物理意義 A = rand(3,5) rows = size(A, 1) cols = size(A, 2)%% III. MATLAB數據類型 %% % 1. 數字 2 + 410 - 73 * 58 / 2%% % 2. 字符與字符串 s = 'a' abs(s) char(65) num2str(65)str = 'I Love MATLAB & Machine Learning.'length(str)doc num2str%% % 3. 矩陣 A = [1 2 3; 4 5 2; 3 2 7] B = A'%轉置 C = A(:)%列向量 D = inv(A)%逆 A * DE = zeros(10,5,3) E(:,:,1) = rand(10,5) E(:,:,2) = randi(5, 10,5) E(:,:,3) = randn(10,5)%% % 4. 元胞數組 A = cell(1, 6) A{2} = eye(3) A{5} = magic(5) B = A{5}%% % 5. 結構體 books = struct('name',{{'Machine Learning','Data Mining'}},'price',[30 40]) books.name books.name(1) books.name{1}%% IV. MATLAB矩陣操作 %% % 1. 矩陣的定義與構造 A = [1 2 3 5 8 5 4 6] B = 1:2:9 C = repmat(B, 3, 1)%復制,將B復制成3行1列 D = ones(2, 4)%% % 2. 矩陣的四則運算 A = [1 2 3 4; 5 6 7 8] B = [1 1 2 2; 2 2 1 1] C = A + B D = A - B E = A * B'%矩陣進行計算 F = A .* B具體數值進行計算 G = A / B % G * B = A G * B * pinv(B) = A * pinv(B) G = A * pinv(B) H = A ./ B%% % 3. 矩陣的下標 A = magic(5) B = A(2,3) C = A(3,:) D = A(:,4) [m, n] = find(A > 20)%% V. MATLAB邏輯與流程控制 %% % 1. if ... else ... end A = rand(1,10) limit = 0.75;B = (A > limit); % B is a vector of logical values if any(B)fprintf('Indices of values > %4.2f: \n', limit);disp(find(B))%dis輸出 elsedisp('All values are below the limit.') end%% % 2. for ... end k = 10; hilbert = zeros(k,k); % Preallocate matrixfor m = 1:kfor n = 1:khilbert(m,n) = 1/(m+n -1);end end hilbert%% % 3. while ... end n = 1; nFactorial = 1; while nFactorial < 1e100n = n + 1;nFactorial = nFactorial * n; end nfactorial(69) factorial(70)prod(1:69) prod(1:70)%% % 4. switch ... case ... end mynumber = input('Enter a number:');switch mynumbercase -1disp('negative one');case 0disp('zero');case 1disp('positive one');otherwisedisp('other value'); end%% VI. MATLAB腳本與函數文件 %% % 1. 腳本文件 myScript%% % 2. 函數文件 mynumber = input('Enter a number:'); output = myFunction(mynumber)%% VII. MATLAB基本繪圖操作 %% % 1. 二維平面繪圖 x = 0:0.01:2*pi; y = sin(x); figure plot(x, y) title('y = sin(x)') xlabel('x') ylabel('sin(x)') xlim([0 2*pi])x = 0:0.01:20; y1 = 200*exp(-0.05*x).*sin(x); y2 = 0.8*exp(-0.5*x).*sin(10*x); figure [AX,H1,H2] = plotyy(x,y1,x,y2,'plot');%兩個縱坐標 set(get(AX(1),'Ylabel'),'String','Slow Decay') set(get(AX(2),'Ylabel'),'String','Fast Decay') xlabel('Time (\musec)') title('Multiple Decay Rates') set(H1,'LineStyle','--') set(H2,'LineStyle',':')%% % 2. 三維立體繪圖 t = 0:pi/50:10*pi; plot3(sin(t),cos(t),t) xlabel('sin(t)') ylabel('cos(t)') zlabel('t') grid on axis square%% % 3. 圖形的保存與導出% (1) Edit → Copy Figure % (2) Toolbar → Save % (3) print('-depsc','-tiff','-r300','picture1') % (4) File → Export Setup%% VIII. MATLAB文件導入 %% % 1. mat格式 save data.mat x y1 y2 clear all load data.mat%% % 2. txt格式 M = importdata('myfile.txt');S = M.data; save 'data.txt' S -ascii T = load('data.txt');isequal(S, T)%% % 3. xls格式 xlswrite('data.xls',S) W = xlsread('data.xls'); isequal(S, W)xlswrite('data.xlsx',S) U = xlsread('data.xlsx'); isequal(S, U)%% % 4. csv格式 csvwrite('data.csv',S) V = csvread('data.csv'); isequal(S, V)m函數文件
function output = myFunction(input)switch inputcase -1output = 'negative one';case 0output = 'zero';case 1output = 'positive one';otherwiseoutput = 'other value'; endend mynumber = input('Enter a number:');switch mynumbercase -1disp('negative one');case 0disp('zero');case 1disp('positive one');otherwisedisp('other value'); end
作者:電氣工程的計算機萌新-余登武
總結
以上是生活随笔為你收集整理的MATLAB-1:入门基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的迭代器,生成器,闭包,装
- 下一篇: MATLAB-2:基础与提高