フィルターのクリア

how to convert integer array to one value??

21 ビュー (過去 30 日間)
Abdalla Mansour
Abdalla Mansour 2015 年 3 月 2 日
コメント済み: Guillaume 2015 年 3 月 3 日
hi , i want to convert array such as x=[1;2;3;4;5] to one value as y=12345

採用された回答

Guillaume
Guillaume 2015 年 3 月 3 日
x = [6 0 8 1 3 0];
validateattributes(x, {'numeric'}, {'integer', 'nonnegative', '<', 10});
y = polyval(x, 10)
  4 件のコメント
Abdalla Mansour
Abdalla Mansour 2015 年 3 月 3 日
thank you Guillaume , Joseph Cheng and Per isakson
Guillaume
Guillaume 2015 年 3 月 3 日
If you're going to go through string conversion, this should be the fastest:
y =str2num(char(x + '0'))
That is add ASCII value of '0' to each number and convert the string back to number.
conversions to / from strings are very slow compared to just multiplications / additions, so my initial solution is probably the most efficient.

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

その他の回答 (1 件)

rantunes
rantunes 2015 年 3 月 3 日
編集済み: Guillaume 2015 年 3 月 3 日
Hey,
You can also use another (more mathematical) approach:
x = [1;2;3;4;5];
y = 0;
for i = 1:length(x)
y = y + (10^(i-1))*x(length(x)+1-i);
end
Greets
  2 件のコメント
rantunes
rantunes 2015 年 3 月 3 日
sorry for the one line copy-paste ;)
Guillaume
Guillaume 2015 年 3 月 3 日
Fixed the formatting for you.
Your code is what polyval does.

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by