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!