function [ sum_m ] = diffSum( )

%function [ sum_m ] = diffSum(beginning ,ending,dim)

tic%用来计时的,没什么作用,纯粹是为了跟大家讲解这么个用法

%利用while,for等语言实现简单求和运算,作者:nicolashe,2016年5月9日 11:13:26

% begin代码起始数,end代表终止数,都是闭口的,dim表示选择用的模式1表示

%表示采用while,2表示采用for,为了展示给你们看,采用switch,也可以使用if实现模式的选择,默认采用1

%%下面利用switch实现选择

%实际上我此时已经忘记了switch的语法,但是不要紧,可以即学即用啊,我查帮助文档得知

%{

help switch

switch Switch among several cases based on expression.

The general form of the switch statement is:

switch switch_expr

CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

The statements following the first CASE where the switch_expr matches

the case_expr are executed. When the case expression is a cell array

(as in the second case above), the case_expr matches if any of the

elements of the cell array match the switch expression. If none of

the case expressions match the switch expression then the OTHERWISE

case is executed (if it exists). Only one CASE is executed and

execution resumes with the statement after the END.

The switch_expr can be a scalar or a string. A scalar switch_expr

matches a case_expr if switch_expr==case_expr. A string

switch_expr matches a case_expr if strcmp(switch_expr,case_expr)

returns 1 (true).

Only the statements between the matching CASE and the next CASE,

OTHERWISE, or END are executed. Unlike C, the switch statement

does not fall through (so BREAKs are unnecessary).

Example:

To execute a certain block of code based on what the string, METHOD,

is set to,

method = 'Bilinear';

switch lower(method)

case {'linear','bilinear'}

disp('Method is linear')

case 'cubic'

disp('Method is cubic')

case 'nearest'

disp('Method is nearest')

otherwise

disp('Unknown method.')

end

Method is linear

See also case, otherwise, if, while, for, end.

Reference page in Help browser

doc switch

%}

%提示输入相关数据

beginning=input('请您输入你要求和的起始数:');

ending=input('请您输入要求和的终止数:');

dim=input('请您输入相关模式,1表示使用while,2表示使用for来实现,dim= ');

%%

% if isempty(dim)

% dim=0;%默认它一个数,否则您没有输入的话,switch会出错;

% else

% dim=dim;

%%

sum_m=zeros(1);%预置一个数来承装最终的和

temp=zeros(1);%预置一个数来作为临时存数器

switch dim

case 1

t1=clock;

temp=beginning;

while (temp<=ending)

sum_m=sum_m+temp;

temp=temp+1;

end

disp(['while程序总运行时间:',num2str(etime(clock,t1))]);

case 2

t1=clock;

for temp=beginning:ending

sum_m=sum_m+temp;

end

disp(['for程序总运行时间:',num2str(etime(clock,t1))]);

otherwise

t1=clock;

sum_m=sum((beginning:ending));

disp(['sum程序总运行时间:',num2str(etime(clock,t1))]);

end

%%

disp(['起始数:',num2str(beginning),'终止数:',num2str(ending),'的和是:',num2str(sum_m)]);

%程序写完了2016年5月9日 11:34:28

toc

end

计时:
function [ sum_m ] = diffSum( )

%function [ sum_m ] = diffSum(beginning ,ending,dim)

tic%用来计时的,没什么作用,纯粹是为了跟大家讲解这么个用法

%{

sdfdsf

sdfdsfdsf

%}

%利用while,for等语言实现简单求和运算,作者:nicolashe,2016年5月9日 11:13:26

% begin代码起始数,end代表终止数,都是闭口的,dim表示选择用的模式1表示

%表示采用while,2表示采用for,为了展示给你们看,采用switch,也可以使用if实现模式的选择,默认采用1

%%下面利用switch实现选择

%实际上我此时已经忘记了switch的语法,但是不要紧,可以即学即用啊,我查帮助文档得知

%{

help switch

switch Switch among several cases based on expression.

The general form of the switch statement is:

switch switch_expr

CASE case_expr,

statement, ..., statement

CASE {case_expr1, case_expr2, case_expr3,...}

statement, ..., statement

...

OTHERWISE,

statement, ..., statement

END

The statements following the first CASE where the switch_expr matches

the case_expr are executed. When the case expression is a cell array

(as in the second case above), the case_expr matches if any of the

elements of the cell array match the switch expression. If none of

the case expressions match the switch expression then the OTHERWISE

case is executed (if it exists). Only one CASE is executed and

execution resumes with the statement after the END.

The switch_expr can be a scalar or a string. A scalar switch_expr

matches a case_expr if switch_expr==case_expr. A string

switch_expr matches a case_expr if strcmp(switch_expr,case_expr)

returns 1 (true).

Only the statements between the matching CASE and the next CASE,

OTHERWISE, or END are executed. Unlike C, the switch statement

does not fall through (so BREAKs are unnecessary).

Example:

To execute a certain block of code based on what the string, METHOD,

is set to,

method = 'Bilinear';

switch lower(method)

case {'linear','bilinear'}

disp('Method is linear')

case 'cubic'

disp('Method is cubic')

case 'nearest'

disp('Method is nearest')

otherwise

disp('Unknown method.')

end

Method is linear

See also case, otherwise, if, while, for, end.

Reference page in Help browser

doc switch

%}

%提示输入相关数据

beginning=input('请您输入你要求和的起始数:');

ending=input('请您输入要求和的终止数:');

dim=input('请您输入相关模式,1表示使用while,2表示使用for来实现,dim= ');

%%

% if isempty(dim)

% dim=0;%默认它一个数,否则您没有输入的话,switch会出错;

% else

% dim=dim;

%%

sum_m=zeros(1);%预置一个数来承装最终的和

temp=zeros(1);%预置一个数来作为临时存数器

switch dim

case 1

t1=clock;

temp=beginning;

while (temp<=ending)

sum_m=sum_m+temp;

temp=temp+1;

end

disp(['while程序总运行时间:',num2str(etime(clock,t1))]);

case 2

t1=clock;

for temp=beginning:ending

sum_m=sum_m+temp;

end

disp(['for程序总运行时间:',num2str(etime(clock,t1))]);

otherwise

t1=clock;

sum_m=sum((beginning:ending));

disp(['sum程序总运行时间:',num2str(etime(clock,t1))]);

end

%%

disp(['起始数:',num2str(beginning),'终止数:',num2str(ending),'的和是:',num2str(sum_m)]);

%程序写完了2016年5月9日 11:34:28

toc

end

函数求和代码 matlab,一个简单求和函数的matlab实现(带程序耗时功能)相关推荐

  1. 一段简单的python代码_一个简单的python写的C/S程序

    前段时间闲来无聊,打算学习下python,看了半个月的书,貌似啥都没学会.有个朋友让说,让我帮他写个批量管理linux服务器的程序,我一想就用python来写吧,于是看了下书,然后修修改改,一个soc ...

  2. python绘制一个简单的函数图像使用到了matplotlib库和numpy库

    文章目录 效果展示: 视频链接 实现的思想 使用到的函数包 图片一对应的代码展示 图片二 对应的代码展示 注意事项 效果展示: 视频链接 python绘制一个简单的函数图像(B站视频) 实现的思想 其 ...

  3. 入门攻略丨教你用低代码实现一个简单的页面跳转功能

    一.介绍 HUAWEI DevEco Studio(后文简称:IDE)自2020年9月首次发布以来,经10次迭代升级,不断为HarmonyOS应用开发增强能力.3月31日,IDE再度升级到DevEco ...

  4. 数字信号 fft c源码_如何制作一个简单的人体动态识别微信小程序(附源码)

    知乎小白第一次写专栏,还请多指教. 先放成果. GitHub源码: lrioxh/HAR-applet-of-Wechat​github.com b站演示视频: 居然不需要服务器?!如何制作一个简单的 ...

  5. 用c++写一个简单的钓鱼(集卡)程序

    用c++写一个简单的钓鱼(集卡)程序 因为我们C++的老师要求我们写一个面向对象的C++程序,要求是扑克牌游戏: 1.分花色,牌值表示能显示一副完整的牌 2.洗牌,分牌 3.出牌(游戏规则自己定,可以 ...

  6. 一个简单的BP神经网络matlab程序(附函数详解)

    说明:20180604更新 1.此文的程序来自博客:http://www.cnblogs.com/heaad/archive/2011/03/07/1976443.html 2.本人对其中涉及到的函数 ...

  7. msp430 abs函数 c语言,实现一个简单的msp430软件

    以做一个简单的采集设备为例.假定已经准备好硬件设备,现在我们开始在MCU上搭建运行的软件. 使用的软件为iar ew430 5.10b,仿真器是杭州利尔达的USB型MSP430仿真器LSD-FET43 ...

  8. php mockery单元测试,php - 使用Mockery在模型中测试一个简单的函数 - SO中文参考 - www.soinside.com...

    我完全擅长使用嵌入在Laravel中的Mockery.我很难测试一个简单的模型函数,它增加了引用的一部分,无论我传递给测试结果的值是否正确,即使它应该失败.我想我在某处犯了错误或者我不懂文档.谢谢你的 ...

  9. 使用Python代码实现一个简单的分子动力学模拟程序

    1. 前言 理解分子动力学模拟最好的方法是编写一个分子动力学程序,换句话说,教会计算机去理解并做执行,自己才算理解会了.因此本文将从常用于描述分子间的非键相互作用中的Lennard-Jones pot ...

  10. python博弈论代码_使用 40 多行的 Python 代码实现一个简单的演化过程

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. 在纳米比亚的 PyCon 会议上,我发表了一篇名为 <使用 Python 解决"升级版的剪 ...

最新文章

  1. DeepMind助力Waymo!提升自动驾驶AI准确率,还能加快模型训练
  2. 腾讯清新云计算数据中心主体工程明年初竣工
  3. JAVA入门[23]-SpringBoot配置Swagger2
  4. 介绍语义HTML5元素(感觉这个html5是一个不错的方向!)
  5. Hibernate之Criteria查询
  6. 编程语言入门及进阶、设计模式、面向对象书籍
  7. 【软考】2020年全国计算机技术与软件专业技术资格考试,网络工程师(中级),考纲
  8. android的抓包工具,安卓抓包工具
  9. 陕西网络培训学院自动学习简易脚本
  10. Redis下载安装 windows版本
  11. 单片机系统电路原理图设计
  12. 文件上传(WebUploader)成功之前自定义裁剪(vue-img-cutter),上传裁剪的图片,并兼容ie
  13. JS Event Propagation (bubbling and capture)
  14. 关于sd卡的读取权限
  15. C# - Entity Framework 对一个或多个实体的验证失败。有关详细信息,请参阅“EntityValidationErrors”属性
  16. iOS 应用内购买(In-App Purchase)之开发
  17. python 发送邮件connect none_python发送邮件(smtplib)
  18. 【机器学习】Decision Tree 决策树算法详解 + Python代码实战
  19. 在安卓手机搭建kali环境,手机变成便携式渗透神器
  20. 平面设计:制作创意头像

热门文章

  1. 总结:硬盘随机读写与顺序读写的性能差异
  2. 【将列表中的每个数据转换成倒数 np.reciprocal()】
  3. 设计网站如何提高版式的设计水平?
  4. 在word中强制换行方法如下
  5. 搜狗新闻文本分析实例代码
  6. html caption 靠左,HTML caption标签 align属性
  7. tbase安全和脱敏
  8. 生信学习—Biostar课程3、4安装使用Entrez Direct和SRA toolkit
  9. 基金知识整理--基金购买那些事(2)
  10. 解决谷歌chrome浏览器双击没反应,不能启动(亲测好用)