{"id":22,"date":"2026-07-22T11:41:03","date_gmt":"2026-07-22T16:41:03","guid":{"rendered":"https:\/\/sts.doit.wisc.edu\/training-materials\/?post_type=manual&#038;p=22"},"modified":"2026-08-06T16:50:50","modified_gmt":"2026-08-06T21:50:50","slug":"matlab-1","status":"publish","type":"manual","link":"https:\/\/sts.doit.wisc.edu\/training-materials\/manual\/matlab-1\/","title":{"rendered":"MATLAB 1: Introduction to Working with Matrices and Arrays"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"h.9x64ypi7mxsg\"><strong>Introduction to MATLAB<\/strong><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.zfd5jpvtmbo\"><strong>Overview<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">MATLAB is a high-level programming environment designed for numerical computing, data analysis, and visualization. It is widely used in applications such as mathematics, signal processing, control design, financial modeling, statistical analysis, and computational biology.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.kur7pbay7w2k\"><strong>Key Features of MATLAB:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Interactive Environment:<\/strong>\u00a0Perform calculations, data analysis, and visualization.<\/li>\n\n\n\n<li><strong>Programming Language:<\/strong>\u00a0Create reusable functions, procedures, and algorithms.<\/li>\n\n\n\n<li><strong>Matrix-Based Computations:<\/strong>\u00a0Almost all MATLAB calculations rely on matrices.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.mki9b18sx5y2\"><strong>MATLAB Interface<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"517\" height=\"444\" src=\"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-content\/uploads\/2025\/07\/matlab1.png\" alt=\"Visualization in MATLAB\" class=\"wp-image-19\" title=\"\" srcset=\"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-content\/uploads\/2025\/07\/matlab1.png 517w, https:\/\/sts.doit.wisc.edu\/training-materials\/wp-content\/uploads\/2025\/07\/matlab1-300x258.png 300w\" sizes=\"auto, (max-width: 517px) 100vw, 517px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The MATLAB interface consists of several panels that help users navigate and utilize its features efficiently. These panels include:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.htfb0pclaaud\"><strong>Command Window<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"images\/image8.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The\u00a0<strong>main screen<\/strong>\u00a0for entering calculations and instructions.<\/li>\n\n\n\n<li>Displays results and errors.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<br><br>&gt;&gt; 2 + 5<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ans = 7<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.7y4n4bhskpk6\"><strong>Workspace<\/strong><\/h3>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"images\/image3.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shows all stored variables, their values, and properties.<\/li>\n\n\n\n<li>Example:<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Assign a value:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; radius = 5<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>View variable in the Workspace.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.k2scezqy0dgv\"><strong>Command History<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Logs all executed commands for reference or re-execution.<\/li>\n\n\n\n<li>Example:<\/li>\n\n\n\n<li>Access previous commands using the\u00a0<strong>up arrow key<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.gdyhubj4hw1g\"><strong>Current Folder<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Displays files and scripts accessible to MATLAB.<\/li>\n\n\n\n<li>Example:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\" data-line=\"\">Change the directory:&lt;br&gt;matlab&lt;br&gt;&lt;br&gt;&gt;&gt; cd(&#039;new_directory_path&#039;)<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.9hve27dtaawj\"><strong>M Code<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.2mik69i427hs\"><strong>Basic Operators<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MATLAB supports basic arithmetic and more advanced mathematical operations.<br>Examples:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Basic operations:<br><br>>> 9000 &#8211; 523<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">ans = 8477<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Trigonometric functions:<br><br><br>>> sin(pi\/2)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">ans = 1<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Built-in constants:<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li>pi\u00a0&#8211; Value of \u03c0.<\/li>\n\n\n\n<li>inf\u00a0&#8211; Infinity.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.j5oiqhb6bh73\"><strong>Storing Variables<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Assign values to variables for reuse:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Suppress output with a semicolon (;).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; mass = 50;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.cvf5gm95k9s3\"><strong>Arrays, Vectors, and Matrices<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.cnst76b4tgi7\"><strong>Arrays<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An array stores multiple values under one variable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create an array:<br>matlab<br><br>&gt;&gt; array = [1, 2, 3, 4, 5];<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.qi56gs21xuor\"><strong>Matrices<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A matrix is a 2D array with rows and columns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Create a matrix:<br>matlab<br><br>&gt;&gt; numbers = [1:5; 2 4 6 8 10; 1:2:9];<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.3c624u2fue3k\"><strong>Accessing Values<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Access elements in arrays or matrices using indices.<br>matlab<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; array(1) &nbsp;% Access the first element of the array<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ans = 1<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; numbers(2, 4) &nbsp;% Access element in row 2, column 4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">ans = 8<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.b868ms9elgxh\"><strong>Modifying Values<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Change existing values or add new ones:<br>matlab<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; array(1) = 6; &nbsp;% Change first value to 6<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; array(6) = 7; &nbsp;% Add a new value<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.x96gyho2eg0p\"><strong>Operations with Arrays and Matrices<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.gxvxkcfsjf7\"><strong>Concatenation<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Combine arrays or matrices:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; combined = [array, concatenate];<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.pj6setmj5wny\"><strong>Transposition<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Flip the direction of an array:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; transposedArray = array&#8217;;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"images\/image5.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.ff4depwn1xnl\"><strong>Built-In Functions<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">MATLAB provides a variety of functions for array\/matrix operations:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Summing elements:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; sum(array)<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Other common functions:<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Function<\/td><td>Output<\/td><\/tr><tr><td>exp(x)<\/td><td>exponent of x<\/td><\/tr><tr><td>sqrt(x)<\/td><td>square root of x<\/td><\/tr><tr><td>log(x)<\/td><td>equivalent to ln(x)<\/td><\/tr><tr><td>log10(x)<\/td><td>equivalent to log(x)<\/td><\/tr><tr><td>mod(x,y)<\/td><td>modulus of y divided by x<\/td><\/tr><tr><td>round(x)<\/td><td>x rounded to the nearest whole number<\/td><\/tr><tr><td>ceil(x)<\/td><td>rounds each element of X to the nearest integer greater than or equal to that element<\/td><\/tr><tr><td>size(x)<\/td><td>returns the number of rows and columns in the matrix<\/td><\/tr><tr><td>ones(m,n)<\/td><td>creates an m-by-n matrix of ones (m rows, n columns)<\/td><\/tr><tr><td>zeros(m,n)<\/td><td>creates an m-by-nmatrix of zeros<\/td><\/tr><tr><td>length(x)<\/td><td>number of elements in x<\/td><\/tr><tr><td>eye(m,n)<\/td><td>creates an m-by-n matrix with ones on the main diagonal and zeros elsewhere<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.8y2vk3igzaf6\"><strong>Scripts<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.35lv7rocnb90\"><strong>What Are Scripts?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Scripts are MATLAB files containing reusable code or functions, which can be executed in the Command Window.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.me03v0s6840d\"><strong>Creating a Script<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open a new script:<br><img decoding=\"async\" alt=\"\" src=\"images\/image2.png\"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = 2;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A = 4 * pi * r^2;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">V = (4\/3) * pi * r^3;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Save as&nbsp;sphere.m, then execute by pressing&nbsp;<strong>Run<\/strong>&nbsp;or typing:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; sphere<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.nupxid1cavve\"><strong>Plotting<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.w5ewup7bmlw3\"><strong>2D Plotting<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create 2D line graphs using the&nbsp;plot&nbsp;function. Example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; x = 0:pi\/100:2*pi;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; y = sin(x);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; plot(x, y);<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"images\/image4.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.mfvylivxbqbe\"><strong>3D Plotting<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create 3D graphs with built-in functions:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example (Mesh and Surface plots):<br><br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt; [X, Y] = meshgrid(-3:.125:3);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; Z = peaks(X, Y);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; mesh(X, Y, Z); &nbsp;% Wireframe<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"images\/image1.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">&gt;&gt; surf(Z); &nbsp; &nbsp; &nbsp; &nbsp;% Surface<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"images\/image7.png\" alt=\"\" title=\"\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.xlnp0qmegjkn\"><strong>Movies<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.4ovsva1u873a\"><strong>What Are Movies in MATLAB?<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Movies in MATLAB are animated sequences of plots displayed quickly to simulate motion. They are created by generating and storing individual frames of plots.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h.av2zuelbhx2p\"><strong>Creating a Movie<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Create frames for the movie:<br><\/strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for i = 1:length(time)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; plot(distance(:, 1:i)&#8217;);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; axis([1 11 0 35000]);<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&nbsp; &nbsp; f(i) = getframe;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">end<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code explanation:<\/strong><\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>The\u00a0for\u00a0loop iterates through each time step.<\/li>\n\n\n\n<li>The\u00a0plot\u00a0function generates plots from the first to the current time point.<\/li>\n\n\n\n<li>The\u00a0axis\u00a0function ensures consistent axes across frames.<\/li>\n\n\n\n<li>The\u00a0getframe\u00a0function captures the current plot as a frame in the movie.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Play the movie:<\/strong><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;movie(f)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This command plays the sequence of frames stored in the&nbsp;f&nbsp;variable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h.4gnrpzhnuv5v\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Mastering MATLAB requires consistent practice. Use built-in resources, such as the&nbsp;<strong>Help<\/strong>&nbsp;menu, to explore additional functionalities and examples. Continue experimenting with scripts, arrays, and plotting tools to improve your skills.<\/p>\n","protected":false},"author":19,"featured_media":0,"template":"","meta":{"_acf_changed":false,"_uw_seo_meta_title":"","_uw_seo_meta_description":"","_uw_seo_twitter_card_type":"summary_large_image","_uw_seo_meta_image":"","_uw_seo_meta_image_url":"","_uw_seo_meta_image_sizes":[],"_uw_seo_custom_meta_tags":[],"footnotes":""},"categories":[],"class_list":["post-22","manual","type-manual","status-publish","hentry"],"acf":[],"_links":{"self":[{"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/manual\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/manual"}],"about":[{"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/types\/manual"}],"author":[{"embeddable":true,"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/users\/19"}],"version-history":[{"count":0,"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/manual\/22\/revisions"}],"wp:attachment":[{"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sts.doit.wisc.edu\/training-materials\/wp-json\/wp\/v2\/categories?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}