Variables and Expressions 3-1 Matrix Calculation with MATLAB MATLAB® Fundamentals Pony Lai [email protected] © 2012 MathWorks, Inc. 3-2 Variables and Expressions The MATLAB® Desktop Desktop controls Current folder Commands entered & results returned Files in the current folder Variables (data) in memory Previously entered commands Variables and Expressions Interactive Importing 3-3 3-4 Variables and Expressions Read and Write data from EXCEL XLSREAD – – – – – num = xlsread('filename') num = xlsread('filename', -1) num = xlsread('filename', 'sheet') num = xlsread('filename', 'range') [num, txt] = xlsread('filename', 'sheet', 'range') XLSWRITE – – – – xlswrite('filename', xlswrite('filename', xlswrite('filename', xlswrite('filename', 'range') M) M, 'sheet') M, 'range') M, 'sheet', Variables and Expressions Saving and Loading Variables 3-5 3-6 Variables and Expressions Plotting the Data Automatic code 3-7 Variables and Expressions Plot Tools Axis labels -2 Units -1 4/10 Select plot components Add plot elements Change properties 3-8 Variables and Expressions Multiple Plots 1 3 2 Variables and Expressions Exporting to Another Application 3-9 3 - 10 Variables and Expressions Script Files H1 line % % % % % % CALLMODEL Models a blue whale B call. Uses a model of the form y = A.*y0 where A = A0*exp(-B*t).*sin(2*pi*fm*t) and y0 is a sum of harmonics yn = sin(2*pi*n*f0*t) Help % Create the time base for the signal. fs = 4000; t = 0:(1/fs):1.5; Code % Set the fundamental frequency of the call. f0 = 175; Comments % Create the harmonics. y0 = sin(2*pi*f0*t) + sin(2*pi*2*f0*t) + sin(2*pi*3*f0*t); callmodel.m 3 - 11 Variables and Expressions Assignment >> no_of_penguins = x*sin(pi*t); 1. evaluate right-hand side 2. assign resulting value to variable on left-hand side create new variable or overwrite old one, as appropriate 3 - 12 Variables and Expressions Creating Vectors Arbitrary data >> x = [2,3,5,7,11,13]; Equally-spaced values n subdivision points x a b dx >> x = a:dx:b; >> x = linspace(a,b,n); 3 - 13 Variables and Expressions Creating Matrices >> A = [1,2,3; 4,5,6; 7,8,9]; or >> A = [1 2 3; 4 5 6; 7 8 9]; or >> A = [1 2 3 4 5 6 7 8 9] data entry mode 1 2 3 4 5 6 7 8 9 3 - 14 Variables and Expressions Multidimensional Arrays i 5 2 7 4 1 3 j 6 8 k >> A(:,:,2) = [5, 6; 7, 8]; >> B = rand(2,2,3); >> A(:,2,1) ans = 2 4 Variables and Expressions Matrix Creation Functions 3 - 15 compan eye gallery hadamard hankel hilb invhilb magic ones pascal rand randi randn rosser toeplitz vander wilkinson zeros 3 - 16 Variables and Expressions Row, Column Indexing >> gasprices(1,2) >> gasprices(3,6) >> gasprices(end,2) >> gasprices(2,end) >> gasprices(end,end) 3 - 17 Variables and Expressions Multiple Row, Column Indices >> Year = gasprices(:,1) >> gasprices([3,4],6:9) >> gp08 = gasprices(end,2:end) 3 - 18 Variables and Expressions Indexed Assignment >> x = gasprices(1,2) reference >> x = 1.96; >> gasprices(1,2) = x assignment 3 - 19 Variables and Expressions Characters and Strings >> y = x >> y = 'x' variable character >> MarkA = 'Friends, Romans, countrymen, lend me your ears'; 1-by-46 char array 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 'F' 'r' 'i' 'e' 'n' 'd' 's' ',' ' ' 'R' 'o' 'm' 'a' 'n' 's' ',' ' ' 'c' >> FriendNationality = MarkA(10:15) 3 - 20 Variables and Expressions Determining Size Prices size length numel 1.96 1.96 1.89 1.73 1.84 1.95 2.12 2.05 1.63 1.72 1.94 1.71 1.76 2.19 2.72 3.23 3.54 3.85 4.45 1.87 1.92 1.73 1.57 1.45 1.53 1.61 1.62 1.38 1.52 1.86 1.72 1.69 1.99 2.37 2.89 3.26 3.59 4.08 3.63 3.45 3.56 3.41 3.59 4.26 4.41 4.00 3.87 3.85 3.80 3.51 3.62 4.35 4.99 5.46 5.88 6.60 7.51 2.65 2.90 3.27 3.07 3.52 3.96 3.94 3.53 3.34 3.42 3.45 3.40 3.67 4.59 5.24 5.66 6.03 6.88 7.75 4.59 4.50 4.53 3.68 3.70 4.00 4.39 4.07 3.84 3.87 3.77 3.57 3.74 4.53 5.29 5.74 6.10 6.73 7.63 3.16 3.46 3.58 4.16 4.36 4.43 3.64 3.26 2.82 3.27 3.65 3.27 3.15 3.47 3.93 4.28 4.47 4.49 5.74 1.00 1.30 1.50 1.56 1.48 1.11 1.25 1.47 1.49 1.79 2.01 2.20 2.24 2.04 2.03 2.22 2.31 2.40 2.45 2.05 2.49 2.65 2.88 2.87 2.94 3.18 3.34 3.04 3.80 4.18 3.76 3.84 4.11 4.51 5.28 5.92 6.21 5.83 2.82 3.01 3.06 2.84 2.99 3.21 3.34 3.83 4.06 4.29 4.58 4.13 4.16 4.70 5.56 5.97 6.36 7.13 7.42 1.16 1.14 1.13 1.11 1.11 1.15 1.23 1.23 1.06 1.17 1.51 1.46 1.36 1.59 1.88 2.30 2.59 2.80 3.27 [19, 10] max 19 190 * 3 - 21 Variables and Expressions Matrix Operations WAvgPrices 215.4 226.6 235.0 233.4 241.4 252.4 252.1 244.7 227.1 247.7 271.9 257.1 257.5 293.1 334.9 376.0 406.2 440.1 497.1 Prices = 1.96 1.96 1.89 1.73 1.84 1.95 2.12 2.05 1.63 1.72 1.94 1.71 1.76 2.19 2.72 3.23 3.54 3.85 4.45 1.87 1.92 1.73 1.57 1.45 1.53 1.61 1.62 1.38 1.52 1.86 1.72 1.69 1.99 2.37 2.89 3.26 3.59 4.08 3.63 3.45 3.56 3.41 3.59 4.26 4.41 4.00 3.87 3.85 3.80 3.51 3.62 4.35 4.99 5.46 5.88 6.60 7.51 2.65 2.90 3.27 3.07 3.52 3.96 3.94 3.53 3.34 3.42 3.45 3.40 3.67 4.59 5.24 5.66 6.03 6.88 7.75 4.59 4.50 4.53 3.68 3.70 4.00 4.39 4.07 3.84 3.87 3.77 3.57 3.74 4.53 5.29 5.74 6.10 6.73 7.63 3.16 3.46 3.58 4.16 4.36 4.43 3.64 3.26 2.82 3.27 3.65 3.27 3.15 3.47 3.93 4.28 4.47 4.49 5.74 1.00 1.30 1.50 1.56 1.48 1.11 1.25 1.47 1.49 1.79 2.01 2.20 2.24 2.04 2.03 2.22 2.31 2.40 2.45 2.05 2.49 2.65 2.88 2.87 2.94 3.18 3.34 3.04 3.80 4.18 3.76 3.84 4.11 4.51 5.28 5.92 6.21 5.83 2.82 3.01 3.06 2.84 2.99 3.21 3.34 3.83 4.06 4.29 4.58 4.13 4.16 4.70 5.56 5.97 6.36 7.13 7.42 1.16 1.14 1.13 1.11 1.11 1.15 1.23 1.23 1.06 1.17 1.51 1.46 1.36 1.59 1.88 2.30 2.59 2.80 3.27 rpop * 2.1 3.7 7.1 9.0 6.6 13.9 11.8 5.3 6.7 33.6 334.9 = 2.72*2.1 + 2.37*3.7 + 4.99*7.1 + 5.24*9.0 + 5.29*6.6 + 3.93*13.9 + 2.03*11.8 + 4.51*5.3 + 5.56*6.7 + 1.88*33.6 3 - 22 Variables and Expressions Mathematical Operations Performed on all elements of a matrix a b sin( a) sin( b) sin c d sin( c) sin( d ) The operation is vectorized 1 3 2 4 sin ™ 0.8415 0.9093 0.1411 0.7568 sin sind sinh asin exp log log2 log10 sqrt nthroot abs angle floor ceil round mod 3 - 23 Variables and Expressions Statistical Operations Performed on an entire vector or on the columns of a matrix a b mean (v1) mean (v2 ) mean c d v1 v2 1 3 2 4 mean Data is in the form: observations x variables 2 3 max min mean median std sum prod diff gradient cumsum cumprod corrcoef cov filter conv fft ifft 3 - 24 Variables and Expressions Concatenation >> C = [A,B] C = >> A A = 1 2 3 1 0 0 1 2 3 4 5 6 7 8 9 >> B B = 4 5 6 0 1 0 7 8 9 0 0 1 >> D = [A;B] 1 0 0 0 1 0 0 0 1 D = 1 2 3 4 5 6 7 8 9 1 0 0 0 1 0 0 0 1 []
© Copyright 2026 Paperzz