concatenate all value in a matrix into one number
1 回表示 (過去 30 日間)
古いコメントを表示
採用された回答
Thorsten
2016 年 10 月 17 日
This works even if the numbers are not single digits, i.e., in in {1,2,...,9}
v = [10 20 4];
num = sscanf(sprintf('%d', v), '%f')
1 件のコメント
Guillaume
2016 年 10 月 17 日
Indeed, however if they are single digits (as I assumed), conversion to string and back to number is going to be several order of magnitude slower than simple mathematical manipulation.
Not that it matters for such trivial code.
その他の回答 (1 件)
Guillaume
2016 年 10 月 17 日
v = [1 2 3 4];
assert(all(v >= 0 & v < 10 & mod(v, 1) == 0), 'elements of v are not single digits');
sum(10.^(numel(v)-1:-1:0) .* v)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!