How to append several elements to a single element in an array?

I have an array like:
A = [1 2 3 4 5 6 7 8 9 10];
I want to merge all the elements into a single element, like:
A = [12345678910];
but I cannot see any clear way to do so, as functions like 'reshape' or 'horizcat' simple place all the elements vertically, instead of appending all the values into one long element.
Any help would be really appreciated!
Thanks.

1 件のコメント

Stephen23
Stephen23 2018 年 1 月 31 日
Note to others: see my answer if you want a simpler, faster solution.

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

 採用された回答

Birdman
Birdman 2018 年 1 月 31 日
編集済み: Birdman 2018 年 1 月 31 日

0 投票

%%to see the complete number with all digits
format longg
str2double(strjoin(string(A),''))

6 件のコメント

Patrick Ellison
Patrick Ellison 2018 年 1 月 31 日
I added it to the code:
A = [1 2 3 4 5 6 7 8 9 10];
%to see the complete number with all digits
format long
B = str2double(strjoin(string(A),''));
I am afraid it produces '1.234567891000000e+10' as the value within B, am I implementing it incorrectly?
Birdman
Birdman 2018 年 1 月 31 日
編集済み: Birdman 2018 年 1 月 31 日
Type
format longg
with 2 g. This only changes its display format.
Patrick Ellison
Patrick Ellison 2018 年 1 月 31 日
I am still getting the same result, is there anything else I need to include in my implementation of it?
Birdman
Birdman 2018 年 1 月 31 日
編集済み: Birdman 2018 年 1 月 31 日
No you don't. But be aware of the fact that
12345678910
is displayed in a scientific form of
1.2346e+10
by default. My only aim was to show you that the number is equal to what you want. So the following code:
B = str2double(strjoin(string(A),''));
actually does what you want.
Patrick Ellison
Patrick Ellison 2018 年 1 月 31 日
Ok cheers!
Birdman
Birdman 2018 年 1 月 31 日
You are welcome!

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

その他の回答 (1 件)

Stephen23
Stephen23 2018 年 1 月 31 日
編集済み: Stephen23 2018 年 1 月 31 日

0 投票

Much faster and simpler than using string, strjoin, and str2double:
>> A = [1 2 3 4 5 6 7 8 9 10];
>> N = sscanf(sprintf('%d',A),'%f')
N = 12345678910

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2018 年 1 月 31 日

編集済み:

2018 年 1 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by