MATLAB: When numerical data type changes while assigning new data

data typesMATLAB

I just came across something concerning data types which I find astonishing.
i1 = int8( 10 ); class( i1 )
i1 = 10.1; class( i1 )
i1 is int8 after the first line, but changes to double in the 2nd line
im1 = int8( [1 2 3]); class(im1)
im1(1) = 1.1; class(im1)
im1 is int8 after 1st line and remains int8 in the 2nd line, i.e. the floating point value is casted to int8
Why is the behaviour different depending on the matrix size? Is this explained somewhere in the documentation?
I use R2015b.

Best Answer

"Why is the behaviour different depending on the matrix size?"
It doesn't, and there is nothing astonishing happening at all. You are trying to compare allocating a variable with indexing, which are two totally different operations. Lets have a look at them...
Allocation
Your first example totally reallocates the variable, i.e. replaces the old variable with a new one that is totally independent of the previous one (which is permitted because MATLAB is a weakly-typed language):
>> X = 'hello' % allocate a char to a variable named X
X = hello
>> class(X)
ans = char
>> X = 2 % allocate a double to a variable named X
X = 2
>> class(X)
ans = double
The fact that X already existed is totally irrelevant: X = 2; replaces any already-existing variable entirely, and even uses a new memory location (the old memory location is thus freed up).
"i1 is int8 after the first line, but changes to double in the 2nd line"
No, the variable stored in memory does not "change" type: you created a new variable, and discarded the old one. The two variables are totally unrelated to each other in memory, only by chance you happened to have given them the same name.
Indexing
In contrast indexing takes an existing array and puts some value/s into that array (or gets them out):
>> X(1) = 3
X = 3
>> class(X)
ans = double
>> X(1) = uint8(4)
X = 4
>> class(X)
ans = double
Indexing does not create a new variable in memory (unless you are changing the size of the array, in which case the whole array is copied to a new location): indexing simply allocates value/s to the location/s that you specified. MATLAB does an implicit type conversion if required, to keep the value/s the same (limited by the range of the destination data class, of course).