MATLAB Answers

When numerical data type changes while assigning new data?

1 ビュー (過去 30 日間)
Jette
Jette 2017 年 9 月 26 日
編集済み: Stephen Cobeldick 2017 年 9 月 26 日
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.

採用された回答

Stephen Cobeldick
Stephen Cobeldick 2017 年 9 月 26 日
編集済み: Stephen Cobeldick 2017 年 9 月 26 日
"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).
  3 件のコメント
Guillaume
Guillaume 2017 年 9 月 26 日
convert to the left-most class (when concatenating)
Not at all!. The result is the narrowest of the class being concatenated, wherever it occurs in the concatenation
>> [0.1, uint8(4)]
ans =
1×2 uint8 row vector
0 4
>> [uint8(4), 0.1]
ans =
1×2 uint8 row vector
4 0
There are a lot more double numbers that can't be represented as (u)int64 (all non integers) than there are (u)int64 than can't be represented as numbers (anything above flintmax).
I say it is confusing because matlab is the only language I know that narrows implicitly. All others (Java, C/C++/C# and co., python, ruby) do the opposite. Coming from these, it's something you have to watch out for.

サインインしてコメントする。

その他の回答 (0 件)

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by