Combining two number to one number

36 ビュー (過去 30 日間)
Poul Reitzel
Poul Reitzel 2011 年 11 月 29 日
コメント済み: Image Analyst 2019 年 5 月 19 日
Dear Matlab users
Im facing the problem of combining two seperate numbers into one number. Let's say i have the number 1 and 2 and want to combine them in that order, resulting in 12. Another example could be 4 and 6, yielding 46, or 3, 6 and 7 yielding 367.. Is there any smart or simple way of doing this?
Thanks in advance, Poul

採用された回答

Chandra Kurniawan
Chandra Kurniawan 2011 年 11 月 29 日
a = 1;
b = 2;
c = 3;
result = strcat(num2str(a),num2str(b),num2str(c));
result = str2num(result);

その他の回答 (2 件)

Image Analyst
Image Analyst 2014 年 12 月 28 日
Use sprintf():
a = 1;
b = 2;
c = 3;
str = sprintf('%d%d%d', a, b, c) % If you want a string.
doubleStr = str2double(str) % If you want a double.
  1 件のコメント
Stephen23
Stephen23 2019 年 1 月 10 日
Simpler and more versatile to just have one '%d' specifier:
str = sprintf('%d', a, b, c)

サインインしてコメントする。


Poul Reitzel
Poul Reitzel 2011 年 11 月 29 日
Exactly! Just went here to post my same suggestion! ;) Thanks
  6 件のコメント
A C
A C 2019 年 5 月 19 日
I was talking about this code. It returns 367
b = [3 6 7]
out = b*10.^(numel(b)-1:-1:0)'
I thought this would look like this
out = [30 60 70].^[2; 1; 0]
but it returns a matrix.
Why are the two codes different. How does the first code return a single number?
Image Analyst
Image Analyst 2019 年 5 月 19 日
Well, it would be good if you had given that equation for "out" in your first post so we'd have known about it.
It's because the first vector is a row vector and the second is a column vector (because of the '). And you're using a recent version of MATLAB that does automatic expansion. Look up expansion here or in MATLAB to see what that means.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by