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)
Show older comments
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??
0 Comments
Answers (1)
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
Andrei Bobrov
on 31 Oct 2011
Hi Jan! typo in '3.'
a = [1 2 3 7 8 9 1 2 3 4 5 6];
b = reshape(a, 6,2)';
A = b * transpose(10 .^ (5:-1:0));
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...
See Also
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!