Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
1.2 matlab numerical data
1. 1.2 MATLAB Numerical Data
◼ Types of numerical data
◼ Output format of numerical data
◼ Common mathematical function
1
2. 1. Types of Numerical Data
◼ Integer
◼ Floating Point
◼ Complex
The system allocates different storage unit to each data type.
Therefore, it determines the range of data.
2
3. 1. Types of Numerical Data
(1) Integer
◼ unsigned integer: unsigned 8-bit integer, unsigned 16-bit integer, unsigned 32-bit
integer, unsigned 64-bit integer.
◼ signed integer: signed 8-bit integer, signed 16-bit integer, signed 32-bit integer,
signed 64-bit integer.
3
According to the number of bytes required in storage space, each type
of integer is divided into 8-bit, 16-bit, 32-bit, 64-bit integer, so there are
8 types of integers.
4. 1. Types of Numerical Data
(1) Integer
◼ the value of a unsigned 8-bit integer ranges from 00000000~11111111(0~28-1).
◼ the value of a signed 8-bit integer ranges from 10000000~01111111(-27~27-1).
4
A note of clarification here, since the number of bytes used to represent an
integer is different, the range of data is different.
5. 1. Types of Numerical Data
(1) Integer
>> x=int8(129)
x =
127
>>
x=uint8(129)
x =
129
Note: the maximum value of a signed 8-bit integer is 127.
5
uint8 function can convert numerical data to 8-bit
unsigned integers.
int8 function can convert numerical data to 8-bit
signed integers.
But because the maximum value of the 8-bit signed integer data is
127, it can only be converted to the maximum of 127.
Because the maximum value of 8-bit unsigned integer data is 255,
and 129 does not exceed, the result after the conversionis still 129.
6. (2) Floating point
Floating-point data is divided into single-precision and double-precision types.
Single-precision real numbers require 4 bytes of storage space, while the double-
precision real numbers require 8 bytes of storage space. So the double-precision type has
higher data accuracy.
◼ the single function: to convert other types of data to single-precision.
◼ the double function: to convert other types of data to double-precision.
>> class(4)
ans =
double
>> class(single(4))
ans =
single
6
7. (3) Complex
Complex numbers include real and imaginary part, the real and imaginary parts are double-
precision by default, i and j represent the basic imaginary unit.
◼ The real function: to evaluate the real part of a complex number.
◼ The imag function: to evaluate the imaginary part of a complex number.
>> 6+5i
ans =
6.0000 + 5.0000i
>> 6+5j
ans =
6.0000 + 5.0000i
7
8. Form of the format command:
format specifier
>> format long
>> 50/3
ans =
16.666666666666668
>> format
>> 50/3
ans =
16.6667
2. Output Format of Numerical Data
Note: the format command only affects the output
format of the data without affecting the calculation
and storage of data.
8
9. A =
2
6
4
3
>> B=exp(A)
B =
54.5982
20.0855
7.3891
403.4288
3. Common Mathematical Functions
(1) The form of calling the function is:
function name(the value of the independent variable of the function )
>> A=[4,2;3,6]
Note: In the operation of a function, it acts on each
element of the matrix one by one, so the final result
is a matrix that has the same type of variable.
9
MATLAB provides many common mathematical functions, for example, the trigonometric
functions, the square root function, and the natural logarithmic function, etc.
10. (2) Application of commonly used functions
① The trigonometric functions: some functions use radian measure while some use
degree measure. If the function is in degrees just put a "d" after the function name to
show the difference.
>> sin(pi/2)
ans =
1
>> sind(90)
ans =
1
10
11. ② The abs function can be used for evaluating absolute values of real numbers, modulus
of complex numbers, and ASCII code value of strings.
>> abs(-4)
ans =
4
>> abs(3+4i)
ans =
5
>> abs('a')
ans =
97
11
12. >> round(4.7)
ans =
5
>> fix(-3.2)
ans =
-3
>> floor(3.6)
ans =
3
>> ceil(-3.8)
ans =
-3
③ The functions used to round integers are fix, floor, ceil, andround.
◼ The fix function rounds to the nearest integer
toward zero.
◼ The floor function rounds to the nearest integer
toward negative infinity.
◼ The ceil function rounds to the nearest integer
toward positive infinity.
◼ The round function rounds to the nearest
integer.
12
13. ④ Examples of function applications.
◼ Calculate a three-digit positive integer's ones, tens, and hundreds.
>> m=345;
>> m1=rem(m,10)
m1 =
5
>> m2=rem(fix(m/10),10)
m2 =
4
>> m3=fix(m/100)
m3 =
3
13
To establish variable m, then assign it a value of 345.
To establish variables m, then assign it a value of 345
To call the fix function first, then round the result of
345 divided by 10
To call the rem function to find the remainder of 34 divided
by 10
14. >> x=1:100;
>> k=isprime(x);
>> k1=find(k);
>> p=x(k1)
p =
1 至 13 列
2
14 至
43
3 5 7 11 13 17 19 23 29 31 37 41
25 列
47 53 59 61 67 71 73 79 83 89 97
◼ Find all prime numbers in the [1, 100] interval.
14
To generate the vector x of all integers in the range from 1 to 100
To call the "isprime" function to generate the k vector. The elements in the k
vector are either 1 or 0. To be 1 or 0 depends on whether the element at the
corresponding position of the x vector is a prime number.
To call the "isprime" function to generate the k vector. The elements in the k
vector are either 1 or 0. To be 1 or 0 depends on whether the element at the
corresponding position of the x vector is a prime number
To output all prime numbers in x
15. Please follow me to practice in MATLAB
15
1. Types of Numerical Data
(1) Integer
x=int8(129)
x=uint8(129)
(2) Floating point
the single function: to convert other types of data to single-precision.
the double function: to convert other types of data to double-precision.
class(4)
class(single(4))
(3) Complex
Complex numbers include real and imaginary part, the real and imaginary parts are double-precision by
default, i and j represent the basic imaginary unit.
6+5i
6+5j
real(6+5i);
imag(6+5i)
16. Please follow me to practice in MATLAB
16
2. Output Format of Numerical Data
Form of the format command:
format long
50/3
format
50/3
17. Please follow me to practice in MATLAB
17
3. Common Mathematical Functions
A=[4,2;3,6]
B=exp(A)
sin(pi/2)
sind(90)
abs(-4)
abs(3+4i)
abs('a')
18. Please follow me to practice in MATLAB
18
4. The functions used to round integers are fix, floor, ceil, and round.
round(4.7)
fix(-3.2)
floor(3.6)
ceil(-3.8)
19. Please follow me to practice in MATLAB
19
5. Calculate a three-digit positive integer's ones, tens, and hundreds.
m=345;
m1=rem(m,10)
m2=rem(fix(m/10),10)
m3=fix(m/100)
6. Find all prime numbers in the [1, 100] interval
x=1:100;
k=isprime(x);
k1=find(k); p=x(k1)
20. Class Quiz
1. In the command window input the command:
>> x = int8(130)
the value of x is ( ).
A. 127
B. 129
C. 128
D. -127
Answer:A
20
22. Class Quiz
33. The value of sin(pi/2) is equal to that of sind(90).
A. √
B. X
Answer:A
22
23. Class discussion
1. Complex number
Is there a difference between 3 + 4i and 3 + 4*i in a MATLAB
command? Please verify it on the computer.
Tips:Verify the execution results of the following commands in turn and
summarize the rules.
>> clear
>> i=5;
>> 3+4i
>> 3+4*i
>> 3+4*j
>> 3+4j
23
Answer:i or j are all imaginary units before assignment.
After assignment, if there is a multiplier in the formula for
multiplication, and there is no multiplier, it is still an
imaginary number.
24. Class discussion
2. Prime number
In MATLAB, how to judge whether n is a prime number in a simple
way?
24
Answer:Call function isprime (n), if the return value is 1, it is a prime number; if
the return value is 0, it is not a prime number.
25. Class discussion
3. Mathematical constant e
There is no built-in constant for e (2.718), so how can that value be
obtained in MATLAB?
25
Answer: In MATLAB, exp (1) is used to represent e, and exp (x) is used to
represent the x power of e.
Notes de l'éditeur
Look at the complex numbers which consists of both real and imaginary(/ɪˈmædʒɪnəri/) parts, the real and imaginary parts are double-precision by default. In MATLAB. i and j represent the basic imaginary unit. For example, 6+5i and 6+5j represent the same complex number. For the complex numbers, we can use the real function to evaluate (/ɪˈvæljueɪt/) the real part of a complex number, and use the imag function to evaluate(/ɪˈvæljueɪt/) the imaginary part of a complex number.
In MATLAB, you can use the format(/ˈfɔːmæt/) command to set output format for numeric data. The form of the format command is format specifier(/'spesifaiə/). The format specifier determines the output format of the data. For example, The first command is format long. this command sets output format to long format. The second command outputs a value of 50/3(50 divided by 3). We can see the output is 15 decimal(/ˈdesɪml/) places(/'pleɪsɪz/). The third command is to enter the format command without format specifiers and back to the default output format, which is the short(/ʃɔːt/) format, then output a value of 50/3. The output is four decimal places, so different format specifiers make the data output format different. But it should be noted that the format command only affects(/əˈfekts/) the output format of the data without affecting the calculation and storage of data.
MATLAB provides many common mathematical functions, for example, the trigonometric(/ˌtrɪɡənəˈmetrɪk/) functions, the square root function, and the natural(/ˈnætʃrəl/) logarithmic(/ˌlɒɡəˈrɪðmɪk/) function, etc. The call form of the function is the function name (value of independent variable of the function). The independent variable of the function is specified(/ˈspesɪfaɪd/) as a matrix variable. Of course, It can be scalars(/ˈskeɪlə(r)/). The scalar itself is a trope(/trəʊp/) of the matrix. In the operation of a function, it acts on each element of the matrix one by one, so the final result is a matrix that has the same type of variable.
Next, let's have a look at an example. The first command creates matrix A which is a two-row, two-column(/ˈkɒləm/) matrix. The second command calls the exp function to evaluate(ɪˈvæljueɪt) natural index. The independent variable is matrix A then assign the result of the call to B. We can see that in the end B is also a two-row, two-column matrix. Each element in B is actually(/ˈæktʃuəli/) the natural index of the corresponding element in matrix A.
Next, let's illustrate (/ˈɪləstreɪt/) the application of functions in MATLAB. First, the trigonometric(/ˌtrɪɡənəˈmetrɪk/) functions. Some functions use Radian(/ˈreɪdiən/) Measure(/ˈmeʒə(r)/) while some use Degree Measure. If the function is in degrees just put a "d" after the function name to show(/ʃəʊ/) the difference. For example, enter the first command in the command window to evaluate the value of the sine function of π/2 radians and the second command to evaluate a sine function of 90 degrees. Both commands result in 1.
Third, In MATLAB, the functions used to round are fix(/fɪks/), floor(/flɔː(r)/), ceil and round. It's easy for us to feel confused (/kənˈfjuːzd/)when we use them. The round function rounds to the nearest integer. As for the other 3 functions, the meaning of their names can help you remember how they round. You can take ceil for a ceiling over your head, so it rounds up to an integer. It rounds to the nearest integer toward positive infinity. floor is under your feet, so it rounds down to an integer. It rounds to the nearest integer toward(/tɔːd) minus infinity(ɪnˈfɪnəti). fix means fixed. It rounds to the nearest integer toward zero. That is, rounding off decimals.
Next, let’s use some examples to help you further(/ˈfɜːðə(r)/) understand. The first command round(4.7) rounds to 5. The second command fix(-3.2) rounds to the nearest integer toward zero, the result is -3 The third command floor(3.6) it rounds to the nearest integer toward minus infinity((ɪnˈfɪnəti), so the result is 3 instead of 4. The fourth command ceil(-3.8). It rounds to the nearest integer toward positive infinity, and the result is -3.
Just now, we listed the uses of some common functions , but actually(/ˈæktʃuəli/), there are more functions available(/əˈveɪləbl/) in MATLAB. Let's look at two examples next. The first question is to calculate a three-digit(/ˈdɪdʒɪt/) positive integer's ones, tens, and hundreds. Think about how do you mathematically solve for a three-digit positive(/ˈpɒzətɪv/) integer's ones, tens, and hundreds? The remainder of a three-digit positive integer divided by 10 is the ones of this number. How about the tens? A three-digit positive integer divided by 10 has removed its ones after being rounded. The remainder of the new number divided by 10 is the ones of the new number. That's also the tens of the original(/əˈrɪdʒənl/) number. How do you calculate the hundreds? A three-digit positive integer, after being divided by 100 and rounded up, its ones and tens are removed. So we can get its hundreds. Think about how is it done in MATLAB? Obviously(/ˈɒbviəsli/), rounding functions and remainder functions are needed. The rounding function was mentioned earlier. As for the remainder(/rɪˈmeɪndə(r)/) function, we can use rem function or mod function.
Let’s notice(/ˈnəʊtɪs/) the results of the operation next. The first command is to establish variables m, then assign it a value of 345. The second command calls the rem function to find the remainder of 345 divided by 10 and assign it to m1. The result is 5, the ones of 345. The third command is to call the fix function first, then round the result of 345 divided by 10. The result is 34. Then call the rem function to find the remainder of 34 divided by 10. The result is 4. The tens of 345. The fourth command calls the fix function to round the result of 345 divided by 100. The result is 3. Namely the hundreds of 345.
The second problem is to find all prime(/praɪm/) numbers in the [1, 100] interval(/ˈɪntəvl/). This is a very common problem in programming learning. Usually based on the definition of a prime number, we can use a loop structure(/ˈstrʌktʃə(r)/) to solve this problem. Here is the solution of the MATLAB function. MATLAB provides a function "isprime" to check whether n is a prime number. When n is a prime number, it returns 1, Otherwise returns 0. Using this function and the features (/ˈfiːtʃəz/) of MATLAB data, you can find all the prime numbers between 1 and 100. Let's see the command next. The first command is to generate(/ˈdʒenəreɪt/) the vector x of all integers in the range from 1 to 100. The second command is to call the "isprime" function to generate the k vector. The elements in the k vector are either 1 or 0. To be 1 or 0 depends on whether the element at the corresponding position of the x vector is a prime number. The third command is to call the find function to generate the k1 vector. The elements in the k1 vector are serial(/ˈsɪəriəl) number of non-zero elements of the k vector, That is, the index of the prime element in x. The fourth command is to output all prime numbers in x, Namely all the prime numbers in the range from 1 to 100. By learning this example. We can learn the characteristics and rules(/ruːlz/)of MATLAB data.
Today, I’ve introduced the numeric data of MATLAB. and we've learned about MATLAB data types, features, classification(/ˌklæsɪfɪˈkeɪʃn/), output formats and common internal functions.
That's all for this section. Thank you. I'll see you next time.