<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Matlab on farmer3-c Blog</title><link>https://farmer3-c.github.io/tags/matlab/</link><description>Recent content in Matlab on farmer3-c Blog</description><generator>Hugo</generator><language>zh-cn</language><lastBuildDate>Sat, 22 Nov 2025 08:50:29 +0800</lastBuildDate><atom:link href="https://farmer3-c.github.io/tags/matlab/index.xml" rel="self" type="application/rss+xml"/><item><title>matlab tutorial</title><link>https://farmer3-c.github.io/posts/matlab-tutorial/</link><pubDate>Sat, 22 Nov 2025 08:50:29 +0800</pubDate><guid>https://farmer3-c.github.io/posts/matlab-tutorial/</guid><description>matlab</description><content:encoded><![CDATA[<h1 id="1basic-arithmetic">1.Basic Arithmetic</h1>
<p>直接在命令行输入算式，结果存入ans变量中
如：</p>
<pre tabindex="0"><code>&gt;&gt; 1+2

ans =

     3
</code></pre><h1 id="2variables">2.Variables</h1>
<p>设置变量的值，存入系统中，之后可以调用</p>
<pre tabindex="0"><code>&gt;&gt; x=7

x =

     7

&gt;&gt; x+3

ans =

    10

&gt;&gt; 
</code></pre><h1 id="3change-format">3.Change Format</h1>
<p><strong>format [数据类型]</strong> 可以改变运算的结果数据类型</p>
<pre tabindex="0"><code>&gt;&gt; 1/3

ans =

    0.3333

&gt;&gt; format short
&gt;&gt; 1/3

ans =

    0.3333

&gt;&gt; format long
&gt;&gt; 1/3

ans =

   0.333333333333333

&gt;&gt; 
</code></pre><h1 id="4remove-variables">4.Remove Variables</h1>
<p><strong>clear [变量名]</strong> 用于清楚变量</p>
<pre tabindex="0"><code>&gt;&gt; x

x =

     7

&gt;&gt; clear x
&gt;&gt; x
函数或变量 &#39;x&#39; 无法识别。
 
&gt;&gt; 
</code></pre><h1 id="5clear-specific-variables">5.Clear Specific Variables</h1>
<p>高级一点的Remove Variables之类的用法</p>
<pre tabindex="0"><code>
&gt;&gt; x3=3

x3 =

     3

&gt;&gt; 
&gt;&gt; clear x*
&gt;&gt; x3=3

x3 =

     3

&gt;&gt; who

您的变量为:

ans  x3   

&gt;&gt; whos
  Name      Size            Bytes  Class     Attributes

  ans       1x1                 8  double              
  x3        1x1                 8  double              

&gt;&gt; 
</code></pre><h1 id="6-pre-defined-constants">6. Pre-Defined Constants</h1>
<p>系统中预定义的变量</p>
<pre tabindex="0"><code>&gt;&gt; pi

ans =

   3.141592653589793

&gt;&gt; format short
&gt;&gt; pi

ans =

    3.1416

&gt;&gt; inf

ans =

   Inf

&gt;&gt; nan

ans =

   NaN

&gt;&gt; i

ans =

   0.0000 + 1.0000i
</code></pre><h1 id="7-operational-operators">7. Operational Operators</h1>
<p>操作运算符</p>
<pre tabindex="0"><code>&gt;&gt; 2&gt;3

ans =

  logical

   0

&gt;&gt; 3&gt;2

ans =

  logical

   1

&gt;&gt; 2&gt;2

ans =

  logical

   0

&gt;&gt; 2&gt;=2

ans =

  logical

   1
</code></pre><h1 id="8-built-in-functions">8. Built-In Functions</h1>
<p>内置函数</p>
<pre tabindex="0"><code>&gt;&gt; sin(3.14)

ans =

    0.0016

&gt;&gt; cos(0)

ans =

     1

&gt;&gt; exp(1)

ans =

    2.7183

&gt;&gt; log(10)

ans =

    2.3026

&gt;&gt; abs(-2)

ans =

     2

&gt;&gt; 
</code></pre><h1 id="9-vectors--matrices">9. Vectors &amp; Matrices</h1>
<p>关于向量和矩阵的使用</p>
<pre tabindex="0"><code>&gt;&gt; a=[1 2 3 ;4 5 6 ;7 8 9]

a =

     1     2     3
     4     5     6
     7     8     9

&gt;&gt; whos
  Name      Size            Bytes  Class     Attributes

  a         3x3                72  double              
  ans       1x1                 8  double              
  x3        1x1                 8  double              

&gt;&gt; b=[1 2 3];
&gt;&gt; c=b&#39;

c =

     1
     2
     3

&gt;&gt; whos
  Name      Size            Bytes  Class     Attributes

  a         3x3                72  double              
  ans       1x1                 8  double              
  b         1x3                24  double              
  c         3x1                24  double              
  x3        1x1                 8  double              

&gt;&gt; 
</code></pre><h1 id="10-indexing">10. Indexing</h1>
<p>矩阵、向量的操作</p>
<pre tabindex="0"><code>&gt;&gt; a=[1 2 3;4 5 6;7 8 9]

a =

     1     2     3
     4     5     6
     7     8     9

&gt;&gt; b=a(:,1)

b =

     1
     4
     7

&gt;&gt; a(end,3)=0

a =

     1     2     3
     4     5     6
     7     8     0

&gt;&gt; a(end,:)=0

a =

     1     2     3
     4     5     6
     0     0     0

&gt;&gt; x=[1:5;6:9]
错误使用 vertcat
要串联的数组的维度不一致。
 
&gt;&gt; x=[1:5;6:10]

x =

     1     2     3     4     5
     6     7     8     9    10

&gt;&gt; a1=ones(1,size(a,1))

a1 =

     1     1     1
&gt;&gt; zeros(5)

ans =

     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0
     0     0     0     0     0

&gt;&gt; zeros(1,5)

ans =

     0     0     0     0     0

&gt;&gt; eye(3)

ans =

     1     0     0
     0     1     0
     0     0     1

&gt;&gt; a(1,2)

ans =

     2

&gt;&gt; a(2,2)=10

a =

     1     2     3
     4    10     6
     0     0     0
</code></pre><h1 id="11-other-keywords">11. Other Keywords</h1>
<p>其他的一些关键字</p>
<pre tabindex="0"><code>&gt;&gt; length(a)

ans =

     3

&gt;&gt; size(a)

ans =

     3     3

&gt;&gt; numel(a)

ans =

     9

&gt;&gt; trace(a)

ans =

    11

&gt;&gt; eig(a)

ans =

    0.1849
   10.8151
         0

&gt;&gt; a

a =

     1     2     3
     4    10     6
     0     0     0

&gt;&gt; a=[1:3;4:6;7:9]

a =

     1     2     3
     4     5     6
     7     8     9

&gt;&gt; inv(a)
警告: 矩阵接近奇异值，或者缩放不良。结果可能不准确。RCOND =  1.541976e-18。 
 

ans =

   1.0e+16 *

   -0.4504    0.9007   -0.4504
    0.9007   -1.8014    0.9007
   -0.4504    0.9007   -0.4504

&gt;&gt; det(a)

ans =

   6.6613e-16
</code></pre><h1 id="12-matrix-operations">12. Matrix Operations</h1>
<pre tabindex="0"><code>&gt;&gt; a=rand(5)

a =

    0.2760    0.4984    0.7513    0.9593    0.8407
    0.6797    0.9597    0.2551    0.5472    0.2543
    0.6551    0.3404    0.5060    0.1386    0.8143
    0.1626    0.5853    0.6991    0.1493    0.2435
    0.1190    0.2238    0.8909    0.2575    0.9293

&gt;&gt; b=rand(5)

b =

    0.3500    0.3517    0.2858    0.0759    0.1299
    0.1966    0.8308    0.7572    0.0540    0.5688
    0.2511    0.5853    0.7537    0.5308    0.4694
    0.6160    0.5497    0.3804    0.7792    0.0119
    0.4733    0.9172    0.5678    0.9340    0.3371

&gt;&gt; a+b

ans =

    0.6260    0.8500    1.0371    1.0351    0.9706
    0.8763    1.7906    1.0123    0.6012    0.8231
    0.9062    0.9256    1.2597    0.6694    1.2837
    0.7787    1.1350    1.0795    0.9285    0.2554
    0.5923    1.1410    1.4587    1.1915    1.2664
&gt;&gt; a&gt;b

ans =

  5×5 logical 数组

   1   0   0   0   1
   1   1   1   1   0
   0   1   1   1   1
   1   1   0   0   1
   1   1   1   1   0
&gt;&gt; a.*b

ans =

    0.6173    0.0689    0.1298    0.0623    0.3212
    0.6731    0.0089    0.6744    0.1609    0.0159
    0.0498    0.1514    0.3035    0.7010    0.5488
    0.5987    0.0442    0.4612    0.6300    0.6625
    0.1083    0.0937    0.0276    0.1793    0.5122
&gt;&gt; a^2

ans =

    1.3164    0.9614    0.9676    1.0427    1.2492
    1.5213    1.1350    1.5754    1.5032    1.8462
    2.0937    2.3843    2.5910    2.6654    2.3472
    2.9873    2.2809    2.6699    2.5017    2.4191
    2.7964    2.3417    2.8111    2.6409    2.4855

&gt;&gt; a.^2

ans =

    0.6638    0.0095    0.0248    0.0201    0.4300
    0.8205    0.0776    0.9421    0.1779    0.0013
    0.0161    0.2991    0.9162    0.8386    0.7210
    0.8343    0.9168    0.2356    0.6276    0.8723
    0.3999    0.9310    0.6404    0.9206    0.4607
</code></pre><h1 id="13-solve-system-of-equations">13. Solve System of Equations</h1>
<pre tabindex="0"><code>&gt;&gt; a=[1 2 3;4 5 6;7 8 9]

a =

     1     2     3
     4     5     6
     7     8     9

&gt;&gt; b=[1 1 1]

b =

     1     1     1

&gt;&gt; a\b
错误使用  \ 
矩阵维度必须一致。
 
&gt;&gt; b=b&#39;

b =

     1
     1
     1

&gt;&gt; a\b
警告: 矩阵接近奇异值，或者缩放不良。结果可能不准确。RCOND =  1.541976e-18。 
 

ans =

   -2.5000
    4.0000
   -1.5000
&gt;&gt; x=a\b
警告: 矩阵接近奇异值，或者缩放不良。结果可能不准确。RCOND =  1.541976e-18。 
 

x =

   -2.5000
    4.0000
   -1.5000

&gt;&gt; a*x

ans =

     1
     1
     1
</code></pre><h1 id="14-m-file-scripts">14. M-File Scripts</h1>
<p>matlab文件的使用</p>
<pre tabindex="0"><code>%% matlab tutorial
% December 2025
</code></pre><h1 id="3-magic-cs">3 Magic C’s</h1>
<pre tabindex="0"><code>%% matlab tutorial
% December 2025

clear all
close all
clc
</code></pre><h1 id="15-loops">15. Loops</h1>
<pre tabindex="0"><code>%% matlab tutorial
% December 2025

clear all
close all
clc

counter=10;
% for i=1:5
%     counter=counter+1;
%     disp(counter)
% end

while counter&gt;=5;
    counter=counter-1;
    disp(counter)
end
</code></pre><h1 id="16-plotting">16. Plotting</h1>
<pre tabindex="0"><code>%% matlab tutorial
% December 2025

clear all
close all
clc

counter=10;
% for i=1:5
%     counter=counter+1;
%     disp(counter)
% end

while counter&gt;=5;
    counter=counter-1;
    disp(counter)
end


%% plotting

x=0:0.1:5;
y=x.^2;
plot(x,y,&#39;r+&#39;)
title(&#39;my first plot&#39;)

xlabel(&#39;x_value&#39;)
ylabel(&#39;y_value&#39;)

grid on
hold
y2=x.^3;
y3=x.^4;
plot(x,y2,&#39;g*&#39;)
plot(x,y3)
hold off
legend(&#39;plot1&#39;,&#39;plot2&#39;,&#39;plot3&#39;)

%% subplotting
subplot(311)
plot(x,y)
subplot(312)
plot(x,y2)
subplot(313)
plot(x,y3)
</code></pre><p><img alt="pic1" loading="lazy" src="/img/in-post/matlab_p1.png"></p>
<h1 id="17-functions">17. Functions</h1>
<pre tabindex="0"><code>function a=triangle_area(w,h)
    a=0.5*w*h;
end
</code></pre><h1 id="18-debugging">18. Debugging</h1>
<p>可以设置断点，类似调试c/c++</p>
<hr>
<h1 id="reference">Reference</h1>
<p><a href="https://www.youtube.com/watch?v=tBWMn4y1Yfo&amp;t=1479s">Learn MATLAB in ONE Video!&ndash;by Jousef Murad | Deep Dive</a></p>
]]></content:encoded></item></channel></rss>