MATLAB基础知识【3】

3 阅读6分钟

数值

x = single([5.32 3.47 6.28]) .* 7.5
%转换为单精度数字
x = double([5.32 3.47 6.28]) .* 7.5
%转换为双精度数字
x = int8([5.32 3.47 6.28]) .* 7.5
%转换为8位有符号整数
x = int16([5.32 3.47 6.28]) .* 7.5
%转换为16位有符号整数
x = int16([5.32 3.47 6.28]) .* 7.5
x = int32([5.32 3.47 6.28]) .* 7.5
%由于整数类型不能表示小数部分,所以小数部分会被截断
%[5 3 6] .* 7.5 将每个元素乘以7.5
x = int64([5.32 3.47 6.28]) .* 7.5
x =

   39.900   26.025   47.100

x =

   39.900   26.025   47.100

x =

   38  23  45

x =

   38  23  45

x =

   38  23  45

x =

   38  23  45

最小和最大整数

%显示最小和最大有符号整数数据
str = 'The range for int8 is:\n\t%d to %d ';
sprintf(str, intmin('int8'), intmax('int8'))
%\t 是一个制表符
%intmin('int8') 返回int8数据类型的最小值,即-128
str = 'The range for int16 is:\n\t%d to %d ';
sprintf(str, intmin('int16'), intmax('int16'))

str = 'The range for int32 is:\n\t%d to %d ';
sprintf(str, intmin('int32'), intmax('int32'))

str = 'The range for int64 is:\n\t%d to %d ';
sprintf(str, intmin('int64'), intmax('int64'))
 
%显示最小和最大无符号整数数据
str = 'The range for uint8 is:\n\t%d to %d ';
sprintf(str, intmin('uint8'), intmax('uint8'))

str = 'The range for uint16 is:\n\t%d to %d ';
sprintf(str, intmin('uint16'), intmax('uint16'))

str = 'The range for uint32 is:\n\t%d to %d ';
sprintf(str, intmin('uint32'), intmax('uint32'))

str = 'The range for uint64 is:\n\t%d to %d ';
sprintf(str, intmin('uint64'), intmax('uint64'))
ans = The range for int8 is:
	-128 to 127 
ans = The range for int16 is:
	-32768 to 32767 
ans = The range for int32 is:
	-2147483648 to 2147483647 
ans = The range for int64 is:
	0 to 0 
ans = The range for uint8 is:
	0 to 255 
ans = The range for uint16 is:
	0 to 65535 
ans = The range for uint32 is:
	0 to -1 
ans = The range for uint64 is:
	0 to 18446744073709551616

intmin('int8') 返回int8数据类型的最小值为什么是-128

在计算机科学中,整型数据通常使用二进制补码(Two's Complement)表示法来存储有符号整数。int8类型是一个8位(bit)的有符号整数,意味着它使用8个二进制位来表示一个整数。

8位二进制数可以表示从0000000011111111的256个不同的值。在补码表示法中,最高位(最左边的一位)是符号位,0表示正数,1表示负数。剩下的7位用于表示数值的大小。

对于正数,00000000表示0,00000001表示1,以此类推,直到01111111,它表示2^7 - 1 = 127。

对于负数,10000000表示-128,10000001表示-127,以此类推,直到11111111,它表示-1。

因此,int8类型的最小值是10000000,即-128。这是因为在补码表示法中,最高位的1表示这是一个负数,而剩下的7位全为0表示这是最小的负数。所以,intmin('int8')返回-128,这是int8数据类型可以表示的最小值。同样,int8数据类型可以表示的最大值是01111111,即127。

最小和最大浮点数

函数realmax()和realmin()返回可以用浮点数表示的最大值和最小值。

%显示最小和最大单精度
%浮点数
str = 'The range for single is:\n\t%g to %g and\n\t %g to  %g';
%格式说明符 %g 用于浮点数,它根据数值的大小选择最合适的格式。
sprintf(str, -realmax('single'), -realmin('single'), ...
%realmax('single') 返回单精度浮点数的最大正数
   realmin('single'), realmax('single'))

%显示最小和最大双精度
%浮点数
str = 'The range for double is:\n\t%g to %g and\n\t %g to  %g';
sprintf(str, -realmax('double'), -realmin('double'), ...
%省略号 ... 表示该行代码将在下一行继续
%通过在这些值前加上负号 -,我们可以得到对应的最小负数
   realmin('double'), realmax('double'))
ans = The range for single is:                                                  
        -3.40282e+38 to -1.17549e-38 and                                   %科学计数法中,e 表示“10的指数” 
        %-3.40282e+38 表示 -3.40282 × 10^38    
         1.17549e-38 to  3.40282e+38                                            
ans = The range for double is:                                                  
        -1.79769e+308 to -2.22507e-308 and                                      
         2.22507e-308 to  1.79769e+308

字符串

my_string = 'nhooo''s com';
str_ascii = uint8(my_string)        %8位 ascii 值
%uint8 函数用于将数据转换为 8 位无符号整数。当对一个字符串使用 uint8 函数时,它会将字符串中的每个字符转换为其对应的 ASCII 码值,并返回一个包含这些 ASCII 码值的数组
str_back_to_char= char(str_ascii)  
str_16bit = uint16(my_string)       %16位ascii值
str_back_to_char = char(str_16bit)
str_ascii =

  110  104  111  111  111   39  115   32   99  111  109

str_back_to_char = nhooo's com
str_16bit =

  110  104  111  111  111   39  115   32   99  111  109
str_back_to_char = nhooo's com

矩形字符数组

doc_profile = ['Zara Ali                             '; ...
               'Sr. Surgeon                          '; ...
               'R N Tagore Cardiology Research Center']
doc_profile = char('Zara Ali', 'Sr. Surgeon', ...
                  'RN Tagore Cardiology Research Center')
doc_profile =
Zara Ali                             
Sr. Surgeon                          
R N Tagore Cardiology Research Center
doc_profile =
Zara Ali                            
Sr. Surgeon                         
RN Tagore Cardiology Research Center
name =     'Zara Ali                             ';
position = 'Sr. Surgeon                          '; 
worksAt =  'R N Tagore Cardiology Research Center';
profile = [name ', ' position ', ' worksAt]
profile = strcat(name, ', ', position, ', ', worksAt)
profile = Zara Ali      , Sr. Surgeon      , R N Tagore Cardiology Research Center
profile = Zara Ali,Sr. Surgeon,R N Tagore Cardiology Research Center

profile = [name ', ' position ', ' worksAt]profile = strcat(name, ', ', position, ', ', worksAt) 都是用于将字符串和变量组合成一个新的字符串。但是,它们在实现方式上有一些区别。

  1. 使用方括号 []:

    profile = [name ', ' position ', ' worksAt];
    

    这种方法使用方括号 [] 来连接字符串和变量。方括号在 MATLAB 中用于数组拼接,因此这里实际上是创建了一个字符数组,其中包含了 name, ', ', position, ', ', 和 worksAt 的拼接结果。

  2. 使用 strcat 函数:

    profile = strcat(name, ', ', position, ', ', worksAt);
    

    strcat 函数是专门用于字符串拼接的函数。它将所有输入的字符串和字符数组连接成一个单一的字符串。strcat 在处理字符串拼接时更加高效,因为它直接操作字符串,而不需要创建中间的字符数组。

主要区别:

  • 效率: strcat 通常比使用方括号更高效,因为它专门用于字符串拼接。
  • 可读性: 使用方括号在某些情况下可能更直观,特别是当拼接的字符串和变量较少时。
  • 功能: strcat 只能用于字符串和字符数组的拼接,而方括号可以用于任意数组的拼接。

示例: 假设 name = 'Alice', position = 'Engineer', worksAt = 'CompanyX',那么:

profile = [name ', ' position ', ' worksAt];

profile = strcat(name, ', ', position, ', ', worksAt);

都会生成相同的字符串:

profile = 'Alice, Engineer, CompanyX';

但是,在处理大量字符串拼接时,推荐使用 strcat 以获得更好的性能。

将字符串合并到单元格数组中

name =     'Zara Ali                             ';
position = 'Sr. Surgeon                          '; 
worksAt =  'R N Tagore Cardiology Research Center';
profile = char(name, position, worksAt);
profile = cellstr(profile);
disp(profile)
{                                                                               
   [1,1] = Zara Ali                                                              
   [2,1] = Sr. Surgeon                                                           
   [3,1] = R N Tagore Cardiology Research Center                                 
}