How to combine all columns of an array into one column?

35 ビュー (過去 30 日間)
Ajinkya Bankar
Ajinkya Bankar 2019 年 10 月 4 日
編集済み: Turlough Hughes 2021 年 4 月 28 日
Suppose, If I have three elements in array A = [6, 5, 3] and I want
A = [653] as the only one element. How should I do this?
And I want general solution for this problem as my number of array elements may change but I want single array element.
Kindly help.
Thank you.

採用された回答

Turlough Hughes
Turlough Hughes 2019 年 10 月 4 日
編集済み: Turlough Hughes 2021 年 4 月 28 日
EDIT - More appropriate solution:
A = [6 5 3];
A_new = str2double(sprintf('%d',A))
Original answer (don't do this):
There's probably a more appropriate solution out there but the following should work fine:
A=[6 5 3]
str=num2str(A); %convert to string
str(isspace(str))=''; % remove spaces
A_new=str2num(str); % convert to number
  1 件のコメント
Ajinkya Bankar
Ajinkya Bankar 2019 年 10 月 4 日
It worked. Thank you for the reply.

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

その他の回答 (1 件)

Kelly Kearney
Kelly Kearney 2019 年 10 月 4 日
Are the values in A always going to be less than 10? If not, what answer do you want?
I see two possibilities; treat each value as a string (as in Turlogh's answer):
fun1 = @(x) str2num(regexprep(num2str(x), '\s', ''));
Or treat each digit as ones, tens, hundreds place:
fun2 = @(x) sum(10.^(length(x)-1:-1:0) .* x);
Results:
fun1([6 5 3])
fun2([6 5 3])
fun1([1 10 5])
fun2([1 10 5])
ans =
653
ans =
653
ans =
1105
ans =
205

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by