How do I combine digits in different cells to create one cell containing one number made up of the digits??

2 views (last 30 days)
I have selected the digits from a string, e.g. a = 1 2 3 7 8 9 1 2 3 4 5 6, and I now want to combine a selection of the digit entries to create a reduced array of numbers made up of the digits, e.g. A = [123789, 123456]. How can I do this??

Answers (1)

Jan
Jan on 31 Oct 2011
1. If "a" is a string (synonym for CHAR vector in Matlab):
a = '123789123456';
A = sscanf(a, '%6d'); % Read an integer with 6 characters
2. If "a" is a cell string:
a = {'1', '2', '3', '7', '8', '9', '1', '2', '3', '4', '5', '6'};
make it a string at first and apply the above method:
a = [a{:}];
3. Or is "a" a numerical vector?
a = [1 2 3 7 8 9 1 2 3 4 5 6];
b = transpose(reshape(a, 6, 2)); % [EDITED] Thanks Andrei!
A = b * transpose(10 .^ (5:-1:0));
  3 Comments
Jan
Jan on 31 Oct 2011
@Andrei: We have present 7 different methods, you've check my solutions, found a bug, I've edited it, you have removed your former solution (if I remember correctly).
This is a nice example for inefficient working: Currently the inputs are not well defined...

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!