Troubles with data types: integers, doubles, scientific notation, and type casting
36 views (last 30 days)
I have a vector of integer elements, and most/all elements are in the thousands. When I look at the vector, some numbers are display in the normal format (eg, 5037) but some are displayed in scienfitic notation (eg 4.2890e+03). When I call the isinteger() function on my variable, it returns false. I think it is probably due to the way that some elements are stored in scientific notation, but I havent found any way, looking online, to force the variable to save all elements in the normal format. If I call isinteger(round()) on my variable, it still returns false. I also cannot typecast my vector to be eg. int16 (which I will get to in a minute).
The reason I need integer values is that some of these values will be used to index an array. I am using the "S = sparse(i,j,v,m,n,nz)" function, and the values m,n,nz all come from my variable. If I try to run it as is, where all of the values are actually integers but Matlab does not recognize them as such, I get the error message
Every one of the elements should be integers, but it throws me an error regardless. Just so you can see what my variables are,
n = numel(x);
idx = 1:n;
x is my integer-valued double vector, and k is a particular element from x.
The documentation for sparse indicates that it shouldn't be problematic, as it says the first two terms (i,j aka idx, x) can be "Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical", the third term (v aka 1) can be "Data Types: double | logical", the fourth and fifth terms (m,n aka n,k) can be "Data Types: double" and the last term (nz aka n) can be "Data Types: double"
Based on the documentation, I don't understand why it won't accept my values -- it claims the type "double" is supported for every single input term.
If I try casting my vector, x, to be something else, like an int16, I get the error
even though the documentation claims that it accepts "single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical"
How can I resolve this? Is there any way to force all of the elements in my vector to be saved in the normal format and not scientific notation? Why do I get an error about input types when the documentation claims that data type is supported?
Guillaume on 30 Apr 2020
Edited: Guillaume on 30 Apr 2020
left = 100*round(left,2);
In theory, this should indeed give you integers. Unfortunately, with floating points (double), this is not always going to work and you will get non-integers values. See for example:
>> x = 1.127;
>> y = round(x, 2) %appears to be 1.13. It is not!
>> z = 100*y %appears to be 113. It is not!
>> z - 113 %it is off by a tiny amount
Even though, mathematically, the below is the same, it is much safer:
left = round(left * 100); %do the multiplication before rounding, then round to nearest integer
The reason is that there are many numbers with just two decimals, such as 1.13, that cannot be stored as double. So instead of storing 1.13 matlab stores the nearest possible number (about 1.12999999999999989342...) which of course when multiplied by 100 is not exactly 113 but 112.99999999999998579... (note the change in the latter digits).
Note that this is not dependent on the version of matlab, and actually applies to any code that uses double representation, not just matlab.
More Answers (3)
James Tursa on 29 Apr 2020
Edited: James Tursa on 29 Apr 2020
You are confusing integer "types" with integer "values". Integer types are int8, uint8, ... int16, uint64. Integer values a 1, 2, 3, etc. You can store integer "values" in floating point types (single or double) or in integer types (int8, uint16, etc.).
You are also confusing the stored value with the displayed value. A stored integer value can display as an integer format or can display as a floating point format. But how it is displayed on your screen does not affect the underlying storage value. The display format depends on the value and your display settings.
For your particular problem, you need to provide us with a specific example of where you think you are feeding the sparse( ) function proper inputs (show them to us) and then copy & paste the entire error message for us to see. If sparse( ) is complaining that your index arrays are not integer values, then they aren't. And the data values for a sparse matrix need to be either double or logical ... no other sparse matrix data types are supported.
If all of your inputs are type double, and your indexing arrays have only positive integer values, then things should work for you.
Steven Lord on 30 Apr 2020
Which release of MATLAB are you using? The ability to specify the subscripts (the first two inputs) in the sparse function as arrays of an integer type was added in release R2020a.
From the error message you're receiving, you have at least one element in x that is not an integer value. Find the elements that are not an integer value with:
find(x ~= round(x))