To transpose a matrix, use a single quote ('
):
a'ans = 1 4 7 2 5 8 3 6 10To perform element-wise multiplication (数组元素依次相乘)rather than matrix multiplication, use the
.*
operator:p = a.*ap = 1 4 9 16 25 36 49 64 100Concatenation
Concatenation is the PRocess of joining arrays to make larger ones. In fact, you made your first array by concatenating its individual elements. The pair of square brackets
[]
is the concatenation operator.A = [a,a]A = 1 2 3 1 2 3 4 5 6 4 5 6 7 8 10 7 8 10Concatenating arrays next to one another using commas is called horizontal concatenation. Each array must have the same number of rows. Similarly, when the arrays have the same number of columns, you can concatenate verticallyusing semicolons.
A = [a; a]A = 1 2 3 4 5 6 7 8 10 1 2 3 4 5 6 7 8 10^ Matrix power. Z = X^y is X to the y power if y is a scalar and X is square. If y is an integer greater than one, the power is computed by repeated squaring. For other values of y the calculation involves eigenvalues and eigenvectors. Z = x^Y is x to the Y power if Y is a square matrix and x is a scalar. Computed using eigenvalues and eigenvectors(特征值和特征向量). Z = X^Y, where both X and Y are matrices, is an error. C = mpower(A,B) is called for the syntax 'A ^ B' when A or B is an object.Complex Numbers
Complex numbers have both real and imaginary parts, where the imaginary unit is the square root of-1
.sqrt(-1)ans = 0.0000 + 1.0000iTo represent the imaginary part of complex numbers, use eitheri
orj
.c = [3+4i, 4+3j; -i, 10j]c = 3.0000 + 4.0000i 4.0000 + 3.0000i 0.0000 - 1.0000i 0.0000 +10.0000iArray Indexing
>> A = magic(4)A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1>> help magic magic Magic square. magic(N) is an N-by-N matrix constructed from the integers 1 through N^2 with equal row, column, and diagonal sums. Produces valid magic squares for all N > 0 except N = 2.There are two ways to refer to a particular element in an array. The most common way is to specify row and column subscripts, such as
A(4,2)ans = 14Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:
A(8)ans = 14However, on the left side of an assignment statement, you can specify elements outside the current dimensions. The size of the array increases to accommodate the newcomers.
A(4,5) = 17A = 16 2 3 13 0 5 11 10 8 0 9 7 6 12 0 4 14 15 1 17To refer to multiple elements of an array, use the colon operator(冒号), which allows you to specify a range of the form
start:end
. For example, list the elements in the first three rows and the second column ofA
:A(1:3,2) (column,row)ans = 2 11 7※The colon operator also allows you to create an equally spaced vector of values using the more general form
start:step:end
.If you omit(省略) the middle step, as in
start:end
, MATLAB uses the default step value of1
.B = 0:10:100B = 0 10 20 30 40 50 60 70 80 90 100Workspace Variables
You can view the contents of the workspace using
whos
.whosName Size Bytes Class Attributes A 4x4 128 double B 3x5x2 240 doubleWorkspace variables do not persist after you exit MATLAB. Save your data for later use with the save command,save myfile.matSaving preserves the workspace in your current working folder in a compressed file with a .mat extension, called a MAT-file.To clear all the variables from the workspace, use the clear command.Restore data from a MAT-file into the workspace using load.load myfile.matCharacter Strings
You can concatenate(连接) strings with square brackets, just as you concatenate numeric arrays.
longText = [myText,' - ',otherText]longText =Hello, world - You're right (myText = 'Hello, world'; otherText = 'You''re right')※To convert numeric values to strings, use functions, such as
num2str
orint2str
.f = 71;c = (f-32)/1.8;tempText = ['Temperature is ',num2str(c),'C']tempText =Temperature is 21.6667C求矩阵A的最大值的函数有3种调用格式,分别是: (1) max(A):返回一个行向量,向量的第i个元素是矩阵A的第i列上的最大值。 (2) [Y,U]=max(A):返回行向量Y和U,Y向量记录A的每列的最大值,U向量记录每列最大值的行号。 (3) max(A,[],dim):dim取1或2。dim取1时,该函数和max(A)完全相同;dim取2时,该函数返回一个列向量,其第i个元素是A矩阵的第i行上的最大值。2-D and 3-D Plots
x = 0:pi/100:2*pi;y = sin(x);plot(x,y)xlabel('x')ylabel('sin(x)')title('Plot of the Sine Function')plot(x,y,'r--') By adding a third input argument to theplot
function, you can plot the same variables using a red dashed line.To add plots to an existing figure, usehold
.x = 0:pi/100:2*pi;y = sin(x);plot(x,y)hold ony2 = cos(x);plot(x,y2,':')legend('sin','cos')3-D Plots
新闻热点
疑难解答