storing value in a variable

조회 수: 18 (최근 30일)
ashwin TP
ashwin TP 2020년 9월 7일
편집: Stephen23 2020년 9월 7일
According to my knowledge, the in x we can store values from -127 to 127. then why on executing this code, x=250 and x=3*10^12 is getting stored inx?
int8 x
intmax("int8")
x=35
x=250
x=30000000000000000000

채택된 답변

Stephen23
Stephen23 2020년 9월 7일
편집: Stephen23 2020년 9월 7일
It looks like you are trying to declare a variable type. MATLAB does not have variable type declarations.
This is what your MATLAB code is actually doing:
int8 x % convert the character 'x' to int8.
intmax("int8") % call INTMAX for "int8".
x=35 % define a double with value 35, give it the variable name x
x=250 % define a double with value 250, give it the variable name x
x=30000000000000000000 % define a double with value 3e20, give it the variable name x
None of these commands have anything to do with each other. None of them have any effect on each other.
The first two lines assign their outputs to the default ans. The behavior of the first line is explained here:
Here are the two common ways in MATLAB of converting a value to a particular class, either specify the class:
>> x = int8(250)
x = 127
or using indexing to assign to an existing array of that class:
>> x = zeros(1,1,'int8');
>> x(1) = 250
x = 127

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by