当前位置:网站首页>Matlab rounding

Matlab rounding

2022-06-25 01:18:00 The sheep said it was okay

One 、 Integral function

1. Round to zero ( To round off )

fix- Round to zero (Round towards zero);

fix(3.6)

ans = 3

2. Rounding negative infinity ( No more than x Maximum integer for - Gauss rounding )

floor- Rounding negative infinity (Round towards minus infinity);

floor(-3.6)

ans = -4

3. Rounding to positive infinity ( Greater than x Minimum integer of )

ceil- Rounding to positive infinity (Round towards plus infinity);

ceil(-3.6)

ans = -3

4. Round to the nearest integer , rounding ( Round to the nearest whole )

round- Round to the nearest integer , rounding (Round towards nearest integer);

round(3.5)

ans = 4

Two 、 Round to a certain place after the decimal point , Keep a few decimal places , It's often used .

1. Numerical type roundn— Round any bit position

a=123.4567890;

a=roundn(a,-4)

a = 123.4568

among roundn The functions are as follows :

y = ROUNDN(x) rounds the input data x to the nearest hundredth. % Don't specify n, To the hundredth y = ROUNDN(x,n) rounds the input data x at the specified power % The number of digits specified after the decimal point n

2. Semiotic type

digits(4)

vpa(…)

Must explain :vpa The command does not recognize integers and decimals , Just the total number of digits , So for it, decimal and integer numbers occupy one place , For example 9.3154 If you keep two decimal places, you have to write :

a=9.3154;

digits(3)

b=vpa(a)

b= 9.32

among b Is a symbolic variable ;

3. Character

a=12.34567;

b = fprintf(‘%8.2f’,a)

b = 12.35 among b Is a character variable .

matlab Text output

Two functions :disp fprintf

1、 function disp With just one variable , It can be a conceited matrix or a numerical matrix , To output a simple text message , Just enclose the information in single quotation marks :

disp(‘my favorite color is red’);

perhaps

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

disp([‘your name is’,youname]);

for example

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

enter your name panrq

disp(['your name is ',yourname]);

your name is panrq

When selecting text information with numeric variable values , You need to use the function num2str Convert the type of numeric variable to character type

x=98;

outstring = ['x = ',num2str(x)];

disp(outstring);

x = 98

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

x = 98

disp Function can only take one variable , The columns in the table need to be combined into a matrix , As shown in the following procedure .

x=0:pi/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

Format command

Control the display mode , Until the next format Before it appeared , This article format The order has been in effect .

x=1.23456789;

format short;disp(pi);

3.1416

format long;disp(pi);

3.141592653589793

format short e;disp(pi);

3.1416e+000

format +;disp(pi);

format bank;disp(pi);

      3.14

2、 function fprintf

fprintf(format);

fprintf(format,variables);

fprintf(fid,format,variables);

for example :

fprintf(‘i am concreten’);

i am concrete

a=3;b=‘s’;

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

this is a 3 and s

原网站

版权声明
本文为[The sheep said it was okay]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206242034421569.html