解决方案

matlab中数值的取整、四舍五入、文本输出

seo靠我 2023-09-24 05:47:39

一、取整函数

Matlab取整函数有: fix, floor, ceil, round四种,具体使用方法如下:

(1) fix

朝零方向取整

fix(-1.3)=-1

fix(1.3)=1;

(2) floorSEO靠我

朝负无穷方向取整(不超过x 的最大整数.(高斯取整))

floor(-1.3)=-2

floor(1.3)=1;

(3) ceil

朝正无穷方向取整(大于x 的最小整数)

ceil(-1.3)=-1

ceilSEO靠我(1.3)=2;

(4) round

四舍五入到最近的整数

round(-1.3)=-1

round(-1.52)=-2

round(1.3)=1

round(1.52)=2

二、四舍五入

在小数点后某一位四舍五入SEO靠我,即保留几位小数

1.数值型

roundn—任意位位置四舍五入

a=123.4567890;

a=roundn(a,-4)

a = 123.4568

其中roundn函数功能如下:

y = ROUNDN(x) roSEO靠我unds the input data x to the nearest hundredth. %不指定n,精确到百分位 y = ROUNDN(x,n) rounds the input data xSEO靠我 at the specified power %精确到小数点后指定位数n

2.符号型

digits(4)

vpa(…)

必须说明:vpa命令不能识别整数与小数,只算总位数,因此对它来说小数整数无论哪个都占一SEO靠我位,例如对9.3154保留两位小数时就得写成:

a=9.3154;

digits(3)

b=vpa(a)

b= 9.32

其中b为符号型变量;

3.字符型

a=12.34567;

b = sprintf(’%8.2fSEO靠我’,a)

b = 12.35 其中b为字符型变量。

三、matlab文本输出

两个函数:disp fprintf

1、函数disp

函数disp只带一个变量,他可以是自负矩阵或数值矩阵,要输出简单的文字信息,只SEO靠我需要用单引号将信息括起来:

disp(‘my favorite color is red’);

或者

yourname=input(‘enter your name’,’s’);

disp([‘your naSEO靠我me is’,youname]);

例如

yourname = input(enter your name ,‘s’);

enter your name panrq

disp([your name is ,ySEO靠我ourname]);

your name is panrq

选择带数值变量值的文本信息时,需要用函数num2str将数值变量的类型转换字符型

x=98;

outstring = [x = ,num2str(xSEO靠我)];

disp(outstring);

x = 98

disp([x = ,num2str(x)]);

x = 98

disp函数只能带一个变量,表格中的各列需奥组合成一个矩阵,如下面的程序所示。

x=0:piSEO靠我/5:pi;y=sin(x);

disp([x’ y’]);

0 0

0.6283 0.5878

1.2566 0.9511

1.8850 0.9511

2.5133 0.5878

3.1416 0.0000

ForSEO靠我mat命令

控制显示模式,直到下一个format出现前,这条format命令一直有效。

x=1.23456789;

format short;disp(pi);

3.1416

format long;disp(SEO靠我pi);

3.141592653589793

format short e;disp(pi);

3.1416e+000

format +;disp(pi);

format bank;disp(pi);

3.14

2SEO靠我、函数fprintf

fprintf(format);

fprintf(format,variables);

fprintf(fid,format,variables);

例如:

fprintf(‘i am cSEO靠我oncreten’);

i am concrete

a=3;b=‘s’;

fprintf(‘this is a %d and %s n’,a,b);

this is a 3 and s

————————————SEO靠我————

原文链接:https://blog.csdn.net/u013228046/article/details/40862025

https://www.jianshu.com/p/e4f58b65SEO靠我d076
“SEO靠我”的新闻页面文章、图片、音频、视频等稿件均为自媒体人、第三方机构发布或转载。如稿件涉及版权等问题,请与 我们联系删除或处理,客服邮箱:html5sh@163.com,稿件内容仅为传递更多信息之目的,不代表本网观点,亦不代表本网站赞同 其观点或证实其内容的真实性。

网站备案号:浙ICP备17034767号-2