Beginner digital Mode-matlab Quick start! Part I

Source: Internet
Author: User

MATLAB, the language of grey cattle b everyone knows that encapsulation is good and supports mixed programming. Like what...

OK, now we open matlab

Hint: Official Help documentation is great!

In the MATLAB program, because of the importance of matrix operations, we can be understood as "all variables are vectors."

When we learn the language, many times we start with Hello World, in MATLAB, this program is like this:

Disp (' Hello world! ');

Well, it really just needs a line ...

By your stomach (anyway), we can also assign a value to a variable

A = 2

You can also assign a variable to a vector

A = [1 2 3 4]

Then, try the matrix again.

A = [1 2 3; 4 5 6; 7 8 10]

Hint: Try these commands to see what the program is returning, and then add a semicolon at the end;) Try it!


Workspace : The currently available variables in the workspace (workspace) are called Workspace varibles. For example, we run these two commands:

A = Magic (4); B = rand (3,5,2);

Then you can see them in workspace.

In addition, we can use the whos command at any time to view the properties of the current variable

You can also do this.


matlab operator : Subtraction in the program or with our familiar + 、-、 *,/, but in MATLAB there is also a "left", that is, a\b = b/a.

Matrix multiplication operations can be directly used (*/\) These three symbolic operations, in addition to a bit (. *) symbol to indicate the corresponding position multiplication, point, point (. ^n) the same.

You might as well try it yourself. B = A.*a

output format : Try typing the format Long command, and then enter C = b * INV (b). Change long to short and try again.

Cascade : Two matrices are connected together, generally in two ways-horizontal (c = [A, b]) and longitudinal (c = [A; B]) Two ways

Note : Make a percent sign and write a comment after this line

Complex number : The imaginary part can be expressed in I or J, for example:

c = [3+4i, 4+3j;-I, 10j]

index of the matrix element : For example A is a 4*4 matrix, MATLAB provides two methods of indexing: A (row, column), such as a (4,2) for the 4th row in the 2nd column of the element, and the second is a (index), where index is column down number (and C + + By row from left to right just opposite), such as a (3) for the first column of the third row of elements, a (8) for the 4th row of the 2nd column of the element

Hint: What happens if you try to cross-border index (such as a (17) and A (5,3))?

So what do you do if you want to take a few rows or columns at the same time?

You can use a colon: if a (1:3,2) is the element of the second column of 1-3 rows of matrix A, A (3,:) is the third row of matrix A

Colons can also be used like this:

Start:step:end

If D = 0:10:100 is running, d = [0,10,20,30,40,50,60,70,80,90,100]

save variable to file : Save command, with. Mat as suffix, such as Save Myfile.mat

load variables from file : Load command, such as load Myfile.mat

how to clear : Clear command empties variable, CLC command clears executed command

String: The string manipulation in MATLAB is similar to that in C + + (even the name of the string handler function is the same as C + +). ), this post is recommended here: http://www.cnblogs.com/emanlee/archive/2012/09/13/2683912.html

As with normal variables, strings are also "vectors", so they can also be cascaded. The specific command is: For example at this time B = ' Tencent ', execute a = [' str ', B];a will become ' strtencent '.

f = 71;c = (f-32)/1.8;temptext = [' Temperature is ', Num2str (c), ' C ']

Where the NUM2STR function can convert numbers into string form.

Next, let's take a look at how to draw a 3D image.

2-d Drawing tool--plot function: Let 's start with an example of how to draw an image of Y=sin (x)

x = 0:pi/100:2*pi;y = sin (×);p lot (x, y)

This is the time to see the image. You can then add these commands to beautify

Xlabel (' x ') ylabel (' Sin (x) ') title (' Plot of the Sine Function ')

Hint: Try to change plot (x, y) to plot (x, y, ' r--') to draw the image!

If you want to draw multiple curves on a graph, you can use the hold command

x = 0:pi/100:2*pi;y = sin (x);p lot (y) hold on% continue to plot on this figure z = cos (x);p lot (x,z, ' r--') xlabel (' x ') title (' plotted of the Sine and cosine Function ') Legend (' Sin ', ' cos ')


The --surf/mesh function of the drawing tool :

3D images can be made with the Meshgrid function of a plane, and then according to the function of the equation mapping, you can also use parametric equation mapping.

% do z=x*e^ (-x^2-y^2) image [X, y] = Meshgrid ( -2:.2:2);   % construction Plane z = X. * EXP (-x.^2-y.^2); % Structural graphics subplot (2,1,1); Surf (x, y, z);  Title (' Surf '); % using surf drawing subplot (2,1,2); Mesh (x, y, z);  Title (' Mesh '); % using mesh mapping

Above is the diagram with the Surf function, the following is a mesh function to do the diagram, like which will use that ~

Hint: Check out the subplot function of the official MATLAB document!

Then we use the parametric equation to make a diagram:

t = 0:pi/10:2*pi;    [x, Y, z] = cylinder (4*cos (t)); % is P=4costsubplot (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 programming

Edit files in matlab (if not, automatically create. m files): Edit Calcmean

As for what loops, conditional branching statements, smart you can certainly see at a glance:

%calcmean.m% calculates the average of the number of randomly generated clear;            % Clear Workspace nsamples = 5;npoints = 50; k = 1;while k<=nsamples   % Cycle     iterationstring = [' iteration # ', Int2str (k)];     disp (iterationstring)     currentdata = rand (npoints,1);     samplemean (k)  = mean (CurrentData)         k =  k+1;end% If you remove  k = k+1;  and change the while line to For k = 1:nsamples, Get the same result as Overallmean = mean (Samplemean) 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&nBsp;range '); end 

Press the green "run" in the tool bar to run it! (PS: It feels like python!) )

Hint: Check out the official documentation for the mean function!

Hint: Official documents Quick Check commands have doc and help, such as

Doc Meanhelp mean

Try it separately!


Unconsciously wrote an afternoon, did not think I can write 3400 words of the post, then Matlab Quick start:part I come here!

Reference documents:

[1] the MathWorks, Inc. Matlab? Primer. 2015 (24): 2-31.


Beginner digital Mode-matlab Quick start! Part I

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.