Convert Nx1 matrix into a single number

20 visualizzazioni (ultimi 30 giorni)
Hello
I need to convert many Nx1 matrices into simple intergers. Example:
A=[1 2 3 4 5];
And what I need:
B=12345;
It isn't a problem to make it manually with small matrices, but mines are bigger than 100x1. Can someone help me?
  1 Commento
Stephen23
Stephen23 il 3 Gen 2020
Modificato: Stephen23 il 3 Gen 2020
"I need to convert many Nx1 matrices into simple intergers. ...but mines are bigger than 100x1"
Well that is not going to work. Simple integers only go up to
>> intmax('uint64')
ans = 18446744073709551615
which has exactly 20 digits. If you want to store more than twenty digits, then you cannot use simple integers: you could use character vectors, symbolic integers, or use a special class, e.g.:

Accedi per commentare.

Risposta accettata

John D'Errico
John D'Errico il 3 Gen 2020
Modificato: John D'Errico il 3 Gen 2020
There are probably a gajillion ways to do this trivially.
Since A is a vector of numeric digits, stored as numbers, the simplest is to just use them as numbers. The nice thing is, it will be efficient, and very fast. (Possibly more so than converting them to chars, as others might have you do.) Thus...
A = [1 2 3 4 5];
B = dot(A,10.^(numel(A)-1:-1:0));
B =
12345
Or, one could use tools like base2dec, str2double, str2num, eval, etc. Personally, my favorite from that list might be the short...
base2dec(A +'0',10)
ans =
12345
Having said that, you will not be able to do so, not for numbers with more than 15 digits in general. (You can suceed with SOME 16 digit numbers, but not even most such numbers.) However, you are talking about doing so with 100 digit numbers. A double in MATLAB cannot represent numbers larger than 2^53-1 exactly as integers.
So if you absolutely need to do this, then you absolutely need to use some tool that will allow you to work with large integers. The problem is, those tools are relatively slow. (I'm saying this even about my own VPI tool, as the author.) And then, even then you will be converting those numbers back and forth from a ilst of individual digits to work with them. Instead, you will be far better off if you learn to work with those lists of digits as a list of digits.

Più risposte (2)

Max Murphy
Max Murphy il 3 Gen 2020
A=[1 2 3 4 5];
B=str2double(sprintf('%g',A));
  4 Commenti
Max Murphy
Max Murphy il 3 Gen 2020
Thanks! But I think Stephen raises a good point- I missed that you might have more than 100 digits, in which case my answer would cause an overflow.
Stephen23
Stephen23 il 3 Gen 2020
Modificato: Stephen23 il 3 Gen 2020
Note that this is limited to 16 digits or so, otherwise you will lose information:
>> A = randi(9,1,17)
A =
3 6 5 4 8 6 5 9 3 7 7 4 6 1 1 5 8
>> B = str2double(sprintf('%g',A))
B =
3.654865937746116e+16
>> fprintf('%.0f\n',B)
36548659377461160
>> fprintf('%lu\n',B)
36548659377461160
Also note that the question specified up to 100 digits. There is no way to make this answer work keeping all 100 digits of information.

Accedi per commentare.


Adam Danz
Adam Danz il 3 Gen 2020
Modificato: Adam Danz il 3 Gen 2020
Produce an input vector of 100 single digits
A = randi(9,1,100); % 100 random integers 1:9
% Example: [5 9 2 9 4 5 ... ]
Convert to a character array of 100 characters without spaces
Achar = regexprep(num2str(A),' *','')
% Example: '592945725863239255999239175....'
Or, convert to a large integer
Adouble = str2double(regexprep(num2str(A),' *',''))
% Example: format long; 5.929457258632392e+99
Note that the number must be less than intmax('uint64') as mentioned by Stephen Cobeldick.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by