Convert Nx1 matrix into a single number

조회 수: 20 (최근 30일)
Krzysztof Mazur
Krzysztof Mazur 2020년 1월 3일
편집: Adam Danz 2020년 1월 3일
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
Stephen23
Stephen23 2020년 1월 3일
편집: Stephen23 2020년 1월 3일
"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.:

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2020년 1월 3일
편집: John D'Errico 2020년 1월 3일
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.

추가 답변 (2개)

Max Murphy
Max Murphy 2020년 1월 3일
A=[1 2 3 4 5];
B=str2double(sprintf('%g',A));
  댓글 수: 4
Max Murphy
Max Murphy 2020년 1월 3일
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 2020년 1월 3일
편집: Stephen23 2020년 1월 3일
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.

댓글을 달려면 로그인하십시오.


Adam Danz
Adam Danz 2020년 1월 3일
편집: Adam Danz 2020년 1월 3일
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.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by