码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • MATLAB 2--结构化程式与自定义函数


    目录

    • 一、结构化程式
      • 1、逻辑运算符
      • 2、流程控制语句以及示例
        • ①if语句
        • ②switch语句
        • ③while语句
        • ④for语句
        • ⑤break语句
      • 3、为变量预分配空间(pre-allocating space to variables)
        • ①NOT pre-allocating
        • ②Pre-allocating
    • 二、函数(Funtions)
      • 1、查看内置函数
      • 2、撰写函数
        • ①自由落体
        • ②bubblesort算法
        • ③判断闰年
        • ④华氏温度到摄氏温度的转换

    一、结构化程式

    1、逻辑运算符

    运算符作用
    <小于
    <=小于或等于
    >大于
    >=大于或等于
    ==等于
    ~=不等于
    &&且

    2、流程控制语句以及示例

    流程控制语句作用
    if若if语句为真,则执行子句
    switch根据switch语句内容判断执行哪个子句
    while重复执行子句直到while中的条件为假
    for执行子句固定次数

    ①if语句

    if condition1
         statement1;
    elseif condition2
         statement2;
    else
         statement3;
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    if语句示例1:

    if rem(a,2)==0
        disp('a is even');
    else
        disp('a is odd');
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5

    运行结果: a is odd

    ②switch语句

    switch expression
    case value1
         statement1;
    case value2
         statement2;
    ……
    otherwise
         statement;
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    switch语句示例:2

    switch input_num
    case -1
    	disp('negative 1');
    case 0
    	disp('zero');
    case 1
    	disp('positive 1');
    otherwise
    	disp('other value');
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    运行结果:other value

    ③while语句

    while expression
         statement;
    end
    
    • 1
    • 2
    • 3
    n = 1;
    while prod(1:n) < 1e100    //prod函数用于求数组元素的乘积
    	n = n + 1;
    end
    
    • 1
    • 2
    • 3
    • 4

    运行结果: n=70

    ④for语句

    for variable=start:increment:end
         commands;
    end
    
    • 1
    • 2
    • 3
    for n=1:10
    	a(n)=2^n;
    end
    disp(a)
    
    • 1
    • 2
    • 3
    • 4
          2           4           8          16          32          64         128         256         512        1024
    
    • 1

    ★上述所有循环和条件语句都要在末尾以end闭合

    ⑤break语句

    x = 2; k = 0; error = inf;
    error_threshold = 1e-32;
    while error > error_threshold
        if k > 100
        	break
        end
        x = x - sin(x)/cos(x);
        error = abs(x - pi);
        k = k + 1;
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    3、为变量预分配空间(pre-allocating space to variables)

    ①NOT pre-allocating

    tic
    for ii = 1:2000
        for jj = 1:2000
            A(ii,jj) = ii + jj;
        end
    end
    toc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    程序结果:时间已过 3.754662 秒。

    ②Pre-allocating

    tic
    A = zeros(2000, 2000);		// 预先为变量分配内存空间
    for ii = 1:size(A,1)
        for jj = 1:size(A,2)
            A(ii,jj) = ii + jj;
        end
    end
    toc
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    程序结果:时间已过 0.038823 秒。

    程序一比程序二所用的时间更长.这是因为: 对于程序一,没有预先为变量A分配内存,因此每当A的形状发生改变时,都需要重新为A分配内存地址,这花费了更多的时间。

    二、函数(Funtions)

    1、查看内置函数

    edit(which('mean.m'))
    
    • 1

    在matlab中看到mean的源代码如下:
    在这里插入图片描述

    2、撰写函数

    ①自由落体

    在这里插入图片描述

    function x = freebody(x0,v0,t)
    % calculation of free falling
    % x0: initial displacement in m
    % v0: initial velocity in m/sec
    % t: the elapsed time in sec
    % x: the depth of falling in m
    x = x0 + v0.*t + 1/2*9.8*t.*t;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    使用方式:

    freebody(0, 0, 2)			
    freebody(0, 0, [0 1 2 3])	
    freebody(0, 0, [0 1; 2 3])	
    
    • 1
    • 2
    • 3

    得到 19.6000
    得到 [0 4.9000 19.6000 44.1000]
    得到 [0 4.9000; 19.6000 44.1000]

    ②bubblesort算法

    图1.bubblesort算法代码运行
    在这里插入图片描述运行结果
    在这里插入图片描述

    ③判断闰年

    判断闰年程序
    在这里插入图片描述

    运行程序
    在这里插入图片描述运行程序结果
    在这里插入图片描述

    ④华氏温度到摄氏温度的转换

    function F2C()
    while 1
        F_degree = input('tempreature in Fahrenheit: ', 's');
        F_degree = str2num(F_degree);
        if isempty(F_degree)
            return
        end
        C_degree = (F_degree-32)*5/9;
        disp(['tempreature in Celsius: ' num2str(C_degree)])
    end
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这里是引用


    1. 命令行输入a=5 ↩︎

    2. 命令行输入:input_num=5 ↩︎

  • 相关阅读:
    一篇文章入门Word2Vec
    博云入选 Gartner 中国云原生领域代表性厂商
    go 语言爬虫库goquery介绍
    线程安全案例 --- 线程池
    走进Spark
    【Leetcode合集】9. 回文数
    论文阅读:Graphics2RAW: Mapping Computer Graphics Images to Sensor RAW Images
    Shell 解释器,帮你解析一条Shell语句到底是什么意思
    逆向实战29——某度-某家号2024旋转验证码识别
    python之kafak应用demo
  • 原文地址:https://blog.csdn.net/m0_58585940/article/details/127588716
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号