MATLAB: Convert Nx1 matrix into a single number

convert matrix matrices integer double cell

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 100×1. Can someone help me?

Best Answer

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.
Related Question