why does matlab use 8 bytes to memorize a number like 1 for example
18 次查看(过去 30 天)
显示 更早的评论
hi,
matlab uses 8 bytes to memorize a number like 1 as double as 2 bytes to memorize it as char?
0 个评论
采纳的回答
Walter Roberson
2020-1-3
By default if you do not tell MATLAB what datatype to use for a number, it uses IEEE 754 Double Precision floating point representation, which is described in more detail at https://en.wikipedia.org/wiki/IEEE_754
When you use char, MATLAB stores the values in UTF-16, which is representation that uses a minimum of two bytes per code point, with a system for indicating that more bytes may be needed. https://en.wikipedia.org/wiki/UTF-16 Code points up to roughly 55295 can be represented in two bytes (and there are another 8192 later ones with direct representation). This includes characters up to 힣 but does not include some alphabets such as Linear B or Phonecian. https://en.wikipedia.org/wiki/Plane_(Unicode)#Supplementary_Multilingual_Plane -- it is enough to hold most living languages.
Character manupulation and storage is a lot easier when you (usually) do not need to worry about the number of bytes needed to represent characters. Single-byte characters would miss out on a lot of commonly used characters -- for example the Greek characters do not start until about code position 913.
0 个评论
更多回答(2 个)
Bhaskar R
2020-1-3
MATLAB software takes the defualt value storage memory for numeric a value is double precision floationg value that is 64 bit value in range -2^63 to 2^63-1. When you initialize any value by default it take takes 8 bytes of the memory. In single class memory take 4 bytes.If you want convert default double class value to single class you need to convert explicity as single
x = 1; % by default it is 64 bit(8 byte), double class
whos x
Name Size Bytes Class Attributes
x 1x1 8 double
x = cast(x, 'single') % or x = single(x) % converted 32 bit(4 byte), single class
whos x
Name Size Bytes Class Attributes
x 1x1 4 single
In case of character data type MATALB takes 2 byte of memory for each character
x = '1'; % x is character data type with 1 character
whos x
Name Size Bytes Class Attributes
x 1x1 2 char
x = '123456789'; % x is character data type with 9 character
whos x
Name Size Bytes Class Attributes
x 1x9 18 char
lou ham
2020-1-3
1 个评论
Walter Roberson
2020-1-3
You would typically use uint8 for that purpose. uint8 is an 8 bit unsigned integer, so 0 to 255. See also typecast()
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!