how to avoid scientific notation when dealing with large integers?
17 views (last 30 days)
Show older comments
I have a nucleotide sequence, which obviously is much larger than the one below, but giving that one for a MWE. I use the nt2int function to convert that into an array, and then the polyval function to convert that into a number. I need to further convert this into a string (I know it doesn't make a lot of sense since I started with a string, but I start with a string of characters and now it is a string of digits).
I need this NOT to be in scientific notation so that when I loop through it, the "." of the scientific notation is not passed as part of the string. I am not talking about display here (so format long doesn't work) but about how the computer is reading this.
I tried to convert it into an integer and that didn't work.
So, my question is: how do you pass very large numbers to the computer without using scientific notation? The example makes it clear (hopefully).
TIA
MWE:
motherCell = 'gcgctggtgaaacccatacagaccctcacactgacctgcaccttctctgggttctcactcagc';
motherCell = nt2int(motherCell) %this gives me an array
validateattributes(motherCell, {'numeric'}, {'integer', 'nonnegative', '<', 10});
seq = polyval(motherCell, 10) %this converts the array into a big number
tempSeq = num2str(seq) %convert that number to a string
outputSeq = strings;
length(tempSeq)
for ivals = 1:length(tempSeq)
ivals
tempSeq(ivals)
%do something else
end
seq =
3.23243343111222e+62
tempSeq =
'3.232433431112221e+62'
so here, when I evaluate this, for ivals =2, I get '.' and I cannot perform any additional operation. How do I tell matlab that I don't want it to transform this number into a string in scientific notation, but that I want it to store it as an integer?
0 Comments
Answers (3)
dpb
on 27 Aug 2019
Edited: dpb
on 27 Aug 2019
Well, in base MATLAB you can't have more than
>> intmax('uint64')
ans =
uint64
18446744073709551615
>>
which is only 20 digits long. John d'Errico has an extended precision package on FEX but I've not had the need to actually use it so not sure what limitations are there.
You realize that in the double representation, even despite the conversion, there are only about 15 or maybe 16 actual digits of precision in that value--everything past that is simply rounding and a figment of the i/o conversion routines if you try to print more digits than that.
I have no idea what you intend for polyval to be when passed the array of nucleotide sequence numbers as coefficients for a polynomial of power numel(motherCell)+1, but it's just numerical noise to have raised 10^63 plus 62 more added terms when there are, as noted above, only 15 or so digits of precision in a double.
Whatever you're trying to do here, this is entirely the wrong way to go about it.
0 Comments
Walter Roberson
on 26 Aug 2019
Provided that you have the symbolic toolbox,
sym(sprintf('%d', motherCell))
But you say that you convert into a string, and if that is all you are doing with it then just create the string:
char(motherCell + '0')
1 Comment
Walter Roberson
on 27 Aug 2019
You could also use
digits = '0':'9';
digits(motherCell+1)
Tim DeFreitas
on 27 Aug 2019
If you really need a string of equivalent digits, you can just use num2str on the array of integers, and remove the spaces:
>> erase(num2str(motherCell), " ")
ans =
'323243343111222141213122242121243122432122442424333442421242132'
I'm not sure what you need this for though. You can just as easily write a for loop over the integer array:
for i=1:numel(motherCell)
digit = motherCell(i);
digit_as_char = char(digit);
% Do something with digit or char
end
1 Comment
Walter Roberson
on 27 Aug 2019
char(digit) is not enough unless the digit is in the range 48 to 57. And no loop is needed. That is why I posted char(motherCell + '0')
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!