why does matlab use 8 bytes to memorize a number like 1 for example

18 views (last 30 days)
hi,
matlab uses 8 bytes to memorize a number like 1 as double as 2 bytes to memorize it as char?

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jan 2020
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.

More Answers (2)

Bhaskar R
Bhaskar R on 3 Jan 2020
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
lou ham on 3 Jan 2020
thanks for your responces
so I can use the char type better than double to store or to send a bit steam after compression
  1 Comment
Walter Roberson
Walter Roberson on 3 Jan 2020
You would typically use uint8 for that purpose. uint8 is an 8 bit unsigned integer, so 0 to 255. See also typecast()

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!