matlab官方文档翻译之MATLAB 快速入门

时间:2024-05-20 07:07:36

来源:https://cn.mathworks.com/help/matlab/learn_matlab/desktop.html
这个是从官方网站上一条一条翻译下去的,自己也是初学者在学习。

Desktop Basics桌面基础 {
When you start MATLAB®, the desktop appears in its default layout.
当你启动MATLAB的时候,MATLAB的界面会呈默认布局。
matlab官方文档翻译之MATLAB 快速入门
The desktop includes these panels:
界面中包括这些面板:
Current Folder — Access your files.当前文件夹 — 访问您的文件。
Command Window — Enter commands at the command line, indicated by the prompt (>>).
命令窗口 — 在命令行中输入命令,由提示符(>>)指示。
Workspace — Explore data that you create or import from files.
工作区 — 可以查看你创建的或从文件中导入的数据。

As you work in MATLAB, you issue commands that create variables and call functions. For example, create a variable named a by typing this statement at the command line:
当你使用MATLAB时,你使用命令来创建变量和调用函数。例如,在命令行中键入此语句,创建名为a的变量:
a = 1
MATLAB adds variable a to the workspace and displays the result in the Command Window.
MATLAB将变量a添加到工作区,并在命令窗口中显示结果。
a =
1
Create a few more variables.
创建更多的变量。
b = 2
b =
2
c = a + b
c =
3
d = cos(a)
d =
0.5403
When you do not specify an output variable, MATLAB uses the variable ans, short for answer, to store the results of your calculation.
当你不指定输出变量时,MATLAB使用变量单词answer的缩写ANS来存储计算结果。
sin(a)
ans =
0.8415
If you end a statement with a semicolon, MATLAB performs the computation, but suppresses the display of output in the Command Window.
如果以分号结束语句,则MATLAB执行计算,但不会在命令窗口中显示输出结果。
e = a*b;
You can recall previous commands by pressing the up- and down-arrow keys, ↑ and ↓. Press the arrow keys either at an empty command line or after you type the first few characters of a command. For example, to recall the command b = 2, type b, and then press the up-arrow key.
你能通过按向上和向下箭头键,↑和↓,来重复使用以前的命令。在一个空命令行或键入命令的前几个字符后,按箭头键。例如,要重复命令B = 2,键入B,然后按向上箭头键。
}

Matrices and Arrays矩阵和数组 {
MATLAB is an abbreviation for “matrix laboratory.” While other programming languages mostly work with numbers one at a time, MATLAB® is designed to operate primarily on whole matrices and arrays.
MATLAB是“matrix laboratory”的缩写,而其他编程语言一次只处理一个数字,MATLAB的设计主要是对整个矩阵和数组进行操作。
All MATLAB variables are multidimensional arrays, no matter what type of data. A matrix is a two-dimensional array often used for linear algebra.
所有MATLAB变量都是多维数组,无论哪种类型的数据。矩阵是线性代数中常用的二维数组。

Array Creation数组的创建 {
To create an array with four elements in a single row, separate the elements with either a comma (,) or a space.
若要在一行中创建具有四个元素的数组,可以用逗号(,)或空格分隔元素。
a = [1 2 3 4]
a =
1 2 3 4
This type of array is a row vector.
这种类型的数组是行向量。

To create a matrix that has multiple rows, separate the rows with semicolons.
要创建一个多行的矩阵,用分号分隔每行。
a = [1 2 3; 4 5 6; 7 8 10]
a =
1 2 3
4 5 6
7 8 10

Another way to create a matrix is to use a function, such as ones, zeros, or rand. For example, create a 5-by-1 column vector of zeros.
创建矩阵的另一种方法是使用函数,ones(),zeros(),rand()。例如,创建一个五行一列的零向量。
z = zeros(5,1)
z =
0
0
0
0
0
}
Matrix and Array Operations矩阵与数组运算 {
MATLAB allows you to process all of the values in a matrix using a single arithmetic operator or function.
MATLAB允许您使用单个算术运算符或函数处理矩阵中的所有值。
a + 10
ans =
11 12 13
14 15 16
17 18 20
sin(a)
ans =
0.8415 0.9093 0.1411
-0.7568 -0.9589 -0.2794
0.6570 0.9894 -0.5440

To transpose a matrix, use a single quote (‘):
若要转置矩阵,请使用单引号(’):
a’
ans =
1 4 7
2 5 8
3 6 10

You can perform standard matrix multiplication, which computes the inner products between rows and columns, using the * operator. For example, confirm that a matrix times its inverse returns the identity matrix:
可以执行标准矩阵乘法,它使用*运算符计算行和列之间的内积。例如,一个矩阵乘以它的逆矩阵一定等于单位矩阵:
p = a*inv(a)
p =
1.0000 0 -0.0000
0 1.0000 0
0 0 1.0000
Notice that p is not a matrix of integer values. MATLAB stores numbers as floating-point values, and arithmetic operations are sensitive to small differences between the actual value and its floating-point representation. You can display more decimal digits using the format command:
注意p不是整型的矩阵。MATLAB将数字存储为浮点值,而算术运算对实际值与其浮点表达式之间的细微差别很敏感。可以使用“format”命令来显示更多的十进制数字:
format long
p = a*inv(a)
p =
1.000000000000000 0 -0.000000000000000
0 1.000000000000000 0
0 0 0.999999999999998
Reset the display to the shorter format using
重置显示较短的格式使用
format short
format affects only the display of numbers, not the way MATLAB computes or saves them.
格式只影响数字的显示,而不是MATLAB计算或保存它们的方式。

To perform element-wise multiplication rather than matrix multiplication, use the .* operator:
要执行元素乘法(数量积、点乘)而不是矩阵乘法,使用 .* 运算符:
p = a.*a
p =
1 4 9
16 25 36
49 64 100

The matrix operators for multiplication, division, and power each have a corresponding array operator that operates element-wise. For example, raise each element of a to the third power:
乘法运算、除法运算和幂运算的矩阵运算都有相应的数组运算符来操作元素。例如,将a的每个元素三次方:
a.^3
ans =
1 8 27
64 125 216
343 512 1000
}这里的点乘、叉乘、幂运算我还没搞清楚……
Concatenation级联(矩阵合并){
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 10

Concatenating 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 vertically using semicolons.
串联矩阵的多个用逗号为水平串联。每个数组必须具有相同的行数。同样,当数组有相同的列数,你可以使用分号垂直连接。
A = [a; a]
A =
1 2 3
4 5 6
7 8 10
1 2 3
4 5 6
7 8 10
}
Complex Numbers复数{
Complex numbers have both real and imaginary parts, where the imaginary unit is the square root of -1.
复数具有实部和虚部,虚部为-1的平方根。
sqrt(-1)
ans =
0.0000 + 1.0000i
To represent the imaginary part of complex numbers, use either i or j .
要表示复数的虚数部分,可以使用i或j。
c = [3+4i, 4+3j; -i, 10j]
c =
3.0000 + 4.0000i 4.0000 + 3.0000i
0.0000 - 1.0000i 0.0000 +10.0000i
}

Array Indexing数组索引 {
Every variable in MATLAB® is an array that can hold many numbers. When you want to access selected elements of an array, use indexing.
MATLAB中的每个变量都是一个可以容纳许多数字的数组。当您想要访问数组特定的元素时,使用索引。
For example, consider the 4-by-4 magic square A:
例如,一个4*4的魔方:
A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
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 =
14
Less common, but sometimes useful, is to use a single subscript that traverses down each column in order:
不常见的,但有时有用的,是使用一个单一的下标,按顺序遍历每个列:
A(8)
ans =
14
Using a single subscript to refer to a particular element in an array is called linear indexing.
使用单个下标来引用数组中的特定元素称为线性索引。

If you try to refer to elements outside an array on the right side of an assignment statement, MATLAB throws an error.
如果您试图引用在赋值语句右侧的数组外的元素,MATLAB会抛出一个错误。
test = A(4,5)
Index exceeds matrix dimensions.
索引超过矩阵的范围。
However, 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) = 17
A =
16 2 3 13 0
5 11 10 8 0
9 7 6 12 0
4 14 15 1 17

To 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 of A:
要引用数组的多个元素,请使用冒号运算符,它允许您以起始:结束的形式指定范围。例如,列出前三行和第二行的元素:
A(1:3,2)
ans =
2
11
7
The colon alone, without start or end values, specifies all of the elements in that dimension. For example, select all the columns in the third row of A:
单独的冒号,没有起始值或结束值,指定该维度中的所有元素。例如,选择第三行中的所有列:
A(3,:)
ans =
9 7 6 12 0

The colon operator also allows you to create an equally spaced vector of values using the more general form start:step:end.
冒号运算符还允许你以起始:间距:结束的形式创建一个等间隔值的向量。
B = 0:10:100
B =
0 10 20 30 40 50 60 70 80 90 100

If you omit the middle step, as in start:end, MATLAB uses the default step value of 1.
如果省略中间的间距,如起始:结束,MATLAB使用默认的间距值为1。
}

Workspace Variables工作面板的变量 {
The workspace contains variables that you create within or import into MATLAB® from data files or other programs. For example, these statements create variables A and B in the workspace.
工作面板中包含您在其中创建或者从数据文件或其他程序导入到MATLAB中的变量。例如,这些语句在工作空间中创建变量a和b。
A = magic(4);
B = rand(3,5,2);
You can view the contents of the workspace using whos.
您可以使用whos命令查看工作区的内容。
whos
Name Size Bytes Class Attributes
A 4x4 128 double
B 3x5x2 240 double
The variables also appear in the Workspace pane on the desktop.
这些变量也会出现在桌面上的工作区面板中。
matlab官方文档翻译之MATLAB 快速入门

Workspace variables do not persist after you exit MATLAB. Save your data for later use with the save command,
工作区的变量在退出MATLAB后不存在。保存数据以供以后使用保存命令,
save myfile.mat
Saving preserves the workspace in your current working folder in a compressed file with a .mat extension, called a MAT-file.
保存将当前工作文件夹中的工作区保存在一个以.mat为扩展名的压缩文件中,称为MAT文件。

To clear all the variables from the workspace, use the clear command.
若要清除工作区中的所有变量,请使用clear命令。

Restore data from a MAT-file into the workspace using load.
从mat文件导入数据到工作空间使用load命令。
load myfile.mat
}

Text and Characters文本和字符 {
When you are working with text, enclose sequences of characters in single quotes. You can assign text to a variable.
在处理文本时,用单引号括起字符序列。可以将文本赋值给变量。
myText = ‘Hello, world’;
If the text includes a single quote, use two single quotes within the definition.
如果文本中包含单引号,则在定义中使用两个单引号。
otherText = ‘You”re right’
otherText =
‘You’re right’
myText and otherText are arrays, like all MATLAB® variables. Their class or data type is char, which is short for character.
myText和otherText是数组,如同所有的MATLAB变量。它们的类或数据类型是char,它是character的缩写。
whos myText
Name Size Bytes Class Attributes
myText 1x12 24 char

You can concatenate character arrays with square brackets, just as you concatenate numeric arrays.
你可以将字符数组如矩阵一样用方括号合并。
longText = [myText,’ - ‘,otherText]
longText =
‘Hello, world - You’re right’

To convert numeric values to characters, use functions, such as num2str or int2str.
要将数值转换成字符,使用函数,如num2str int2str
f = 71;
c = (f-32)/1.8;
tempText = [‘Temperature is ‘,num2str(c),’C’]
tempText =
‘Temperature is 21.6667C’
}

Calling Functions调用函数{
MATLAB® provides a large number of functions that perform computational tasks. Functions are equivalent to subroutines or methods in other programming languages.
MATLAB提供了大量执行计算任务的函数。函数相当于其他编程语言中的子程序或方法。
To call a function, such as max, enclose its input arguments in parentheses:
调用函数,如max,将其输入参数括在括号中:
A = [1 3 5];
max(A)
ans = 5
If there are multiple input arguments, separate them with commas:
如果有多个输入的参数,用逗号分隔:
B = [10 6 4];
max(A,B)
ans =
10 6 5

Return output from a function by assigning it to a variable:
通过将函数赋值给变量来返回函数的输出:
maxA = max(A)
maxA = 5

When there are multiple output arguments, enclose them in square brackets:
当有多个输出参数时,将它们括在方括号中:
[maxA,location] = max(A)
maxA = 5
location = 3

Enclose any character inputs in single quotes:
将任何字符输入括在单引号中:
disp(‘hello world’)
hello world

To call a function that does not require any inputs and does not return any outputs, type only the function name:
若要调用不需要任何输入且不返回任何输出的函数,只需键入函数名:
clc
The clc function clears the Command Window.
clc函数用来清空命令行窗口。
}

2-D and 3-D Plots二维和三维图 {
Line Plots线图 {
To create two-dimensional line plots, use the plot function. For example, plot the value of the sine function from 0 to matlab官方文档翻译之MATLAB 快速入门 :
要创建二维线图,就要使用plot函数。例如,将正弦函数的值从0绘制到matlab官方文档翻译之MATLAB 快速入门
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
matlab官方文档翻译之MATLAB 快速入门

You can label the axes and add a title.
您可以对轴进行标记并添加标题。
xlabel(‘x’)
ylabel(‘sin(x)’)
title(‘Plot of the Sine Function’)
matlab官方文档翻译之MATLAB 快速入门

By adding a third input argument to the plot function, you can plot the same variables using a red dashed line.
通过在plot 函数中添加第三个输入参数,可以使用红色虚线绘制相同的变量。
plot(x,y,’r–’)
matlab官方文档翻译之MATLAB 快速入门
The ‘r–’ string is a line specification. Each specification can include characters for the line color, style, and marker. A marker is a symbol that appears at each plotted data point, such as a +, o, or . For example, ‘g:’ requests a dotted green line with * markers.
“r–”字符串是一行规范。每个规范都可以包含线条颜色、样式和标记的字符。标记是在每个绘制的数据点上出现的符号,如:+、o或。例如,’g:‘要求一个带*标记的绿色虚线。

Notice that the titles and labels that you defined for the first plot are no longer in the current figure window. By default, MATLAB® clears the figure each time you call a plotting function, resetting the axes and other elements to prepare the new plot.
注意,为第一个图定义的标题和标签不会再显示在当前图形窗口中。默认情况下,每当你调用绘图函数时,MATLAB都会重置标签和元素来准备新的绘图。
To add plots to an existing figure, use hold.
若要将图添加到现有图形,请使用 hold
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)

hold on

y2 = cos(x);
plot(x,y2,’:’)
legend(‘sin’,’cos’)
matlab官方文档翻译之MATLAB 快速入门
Until you use hold off or close the window, all plots appear in the current figure window.
hold off 或关闭窗口之前,所有的图都会出现在当前的图形窗口中。
}

3-D Plots三维图{
Three-dimensional plots typically display a surface defined by a function in two variables, z = f(x,y) .
三维图通常显示一个函数在两个变量中定义的曲面,z=f(x,y)。
To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid.
对Z,首先创建一个(x,y)点的集合在域的函数使用meshgrid。 (这句实在不懂得怎么翻译了。)
[X,Y] = meshgrid(-2:.2:2);
Z = X . exp(-X.^2 - Y.^2);*
Then, create a surface plot.
然后,创建一个表面图。
surf(X,Y,Z)
matlab官方文档翻译之MATLAB 快速入门
Both the surf function and its companion mesh display surfaces in three dimensions. surf displays both the connecting lines and the faces of the surface in color. mesh produces wireframe surfaces that color only the lines connecting the defining points.
三维绘图用surf 函数及其伴随 mesh 绘制曲面。surf 显示连接线和表面的颜色。mesh 绘制带有颜色连接定义点的线的线框面。 (不会翻译。)
}

Subplots其他 (子图??){
You can display multiple plots in different subregions of the same window using the subplot function.
你可以用subplot 函数在同一窗口不同区域显示多个图。
The first two inputs to***subplot*** indicate the number of plots in each row and column. The third input specifies which plot is active. For example, create four plots in a 2-by-2 grid within a figure window.
subplot 函数的前两个参数指明行和列图的数量。第三个输入指定哪个区域绘图。例如,在2*2格在图形窗口中创建四个图。
t = 0:pi/10:2*pi;
[X,Y,Z] = cylinder(4*cos(t));
subplot(2,2,1); mesh(X); title(‘X’);
subplot(2,2,2); mesh(Y); title(‘Y’);
subplot(2,2,3); mesh(Z); title(‘Z’);
subplot(2,2,4); mesh(X,Y,Z); title(‘X,Y,Z’);
matlab官方文档翻译之MATLAB 快速入门
}
}

Programming and Scripts程序和脚本{
The simplest type of MATLAB® program is called a script. A script is a file with a .m extension that contains multiple sequential lines of MATLAB commands and function calls. You can run a script by typing its name at the command line.
MATLAB程序最简单的类型叫做脚本。脚本是一个含多个连续的MATLAB命令和函数调用以.m扩展的文件。您可以通过在命令行中键入其名称来运行脚本。

Sample Script简单脚本{
To create a script, use the edit command,
要创建脚本,请使用 edit 命令,
edit plotrand
This opens a blank file named plotrand.m. Enter some code that plots a vector of random data:
这会打开一个命名为plotrand.m的空白文件。输入一些产生随机向量的代码:
n = 50;
r = rand(n,1);
plot(r)
Next, add code that draws a horizontal line on the plot at the mean:
接下来,添加在图中绘制水平线的代码:
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title(‘Mean of Random Uniform Data’)
Whenever you write code, it is a good practice to add comments that describe the code. Comments allow others to understand your code, and can refresh your memory when you return to it later. Add comments using the percent (%) symbol.
无论何时编写代码,添加描述代码的注释是一个好习惯。注释允许其他人理解您的代码,并在稍后返回时刷新您的内存。使用百分号(%)符号添加注释。
% Generate random data from a uniform distribution
% and calculate the mean. Plot the data and the mean.

n = 50; % 50 data points
r = rand(n,1);
plot(r)

% Draw a line from (0,m) to (n,m)
m = mean(r);
hold on
plot([0,n],[m,m])
hold off
title(‘Mean of Random Uniform Data’)
Save the file in the current folder. To run the script, type its name at the command line:
将文件保存在当前文件夹中。要运行脚本,请在命令行中键入它的名称:
plotrand
You can also run scripts from the Editor by pressing the Run button, matlab官方文档翻译之MATLAB 快速入门.
你也可以通过点击运行按钮matlab官方文档翻译之MATLAB 快速入门运行脚本。
}

Loops and Conditional Statements循环和条件语句{
Within a script, you can loop over sections of code and conditionally execute sections using the keywords for, while, if, and switch.
在脚本中,可以循环使用部分代码,并有条件地使用关键字(如for, while, if, 和 switch.
)切换执行部分。
For example, create a script named calcmean.m that uses a for loop to calculate the mean of five random samples and the overall mean.
例如,创建一个叫calcmean.m 的脚本,用一个for循环来计算五个随机样本均值和总体均值 。
nsamples = 5;
npoints = 50;

for k = 1:nsamples
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData);
end
overallMean = mean(sampleMean)
Now, modify the for loop so that you can view the results at each iteration. Display text in the Command Window that includes the current iteration number, and remove the semicolon from the assignment to sampleMean.
现在,修改for循环,以便每次迭代时都可以查看结果。在命令窗口中显示文本,包括当前迭代次数,并从分配过程样本删除分号。
for k = 1:nsamples
iterationString = [‘Iteration #’,int2str(k)];
disp(iterationString)
currentData = rand(npoints,1);
sampleMean(k) = mean(currentData)
end
overallMean = mean(sampleMean)
When you run the script, it displays the intermediate results, and then calculates the overall mean.
但你运行这个脚本时,它会显示中间的结果然后计算其总体均值。
calcmean
Iteration #1
sampleMean =
0.3988
Iteration #2
sampleMean =
0.3988 0.4950
Iteration #3
sampleMean =
0.3988 0.4950 0.5365
Iteraion #4
sampleMean =
0.3988 0.4950 0.5365 0.4870
Iteration #5
sampleMean =
0.3988 0.4950 0.5365 0.4870 0.5501
overallMean =
0.4935
In the Editor, add conditional statements to the end of calcmean.m that display a different message depending on the value of overallMean.
在编辑器中,在calcmean.m的末尾添加条件语句用来根据平均值显示不同的信息。
if overallMean < .49
disp(‘Mean is less than expected’)
elseif overallMean > .51
disp(‘Mean is greater than expected’)
else
disp(‘Mean is within the expected range’)
end
Run calcmean and verify that the correct message displays for the calculated overallMean. For example
运行calcmean并验证计根据算出的平均值而正确的消息显示。例如:
overallMean =
0.5178
Mean is greater than expected
}

Script Locations脚本位置{
MATLAB looks for scripts and other files in certain places. To run a script, the file must be in the current folder or in a folder on the search path.
MATLAB在某些地方查找脚本和其他文件。要运行脚本,文件必须位于当前文件夹或搜索路径中的文件夹中。
By default, the MATLAB folder that the MATLAB Installer creates is on the search path. If you want to store and run programs in another folder, add it to the search path. Select the folder in the Current Folder browser, right-click, and then select Add to Path.
默认情况下,MATLAB安装程序创建的MATLAB文件夹位于搜索路径上。如果要在另一个文件夹中存储和运行程序,请将其添加到搜索路径中。在当前文件夹浏览器中选择文件夹,右键单击,然后选择“添加到路径”。
}
}

Help and Documentation帮助和文档{
All MATLAB® functions have supporting documentation that includes examples and describes the function inputs, outputs, and calling syntax. There are several ways to access this information from the command line:
所有MATLAB函数都有支持文档,包括示例,并描述函数输入、输出和调用语法。有几种方法可以从命令行访问此信息:
Open the function documentation in a separate window using the***doc***command.
使用doc命令在单独的窗口中打开函数文档。
Display function hints (the syntax portion of the function documentation) in the Command Window by pausing after you type the open parentheses for the function input arguments.
在命令窗口中显示函数提示(函数文档的语法部分),在输入函数输入参数的打开括号后暂停。
mean(
View an abbreviated text version of the function documentation in the Command Window using the help command.
使用 help 命令在命令窗口中查看函数文档的缩略文本版本。
help mean
Access the complete product documentation by clicking the help icon matlab官方文档翻译之MATLAB 快速入门.
通过点击帮助按钮matlab官方文档翻译之MATLAB 快速入门访问完整的文档。
}
}
说了这么多,你首先要有一个matlab。
http://www.xdowns.com/x/MATLAB/