MATLAB Answers

How can I concatenate two or more arrays without using num2str?

1 ビュー (過去 30 日間)
Adriano
Adriano 2017 年 2 月 20 日
Answered: Adriano 2017 年 2 月 21 日
For example if I have two vertical arrays:
A = [1;2] B = [3;4]
I need this output:
C = [13; 24]
The problem is that my arrays have many rows, thus num2str is too slow. What can I do? Thanks!!!

  2 件のコメント

Adam
Adam 2017 年 2 月 20 日
I can't help but think of the joke of someone asking for directions to somewhere and being told 'Well, if I were you I wouldn't start from here'.
There may be better suggestions based on the wider context of the code than already having A and B defined.
John D'Errico
John D'Errico 2017 年 2 月 20 日
Good response by Adam.

サインイン to comment.

採用された回答

John D'Errico
John D'Errico 2017 年 2 月 20 日
編集済み: John D'Errico 2017 年 2 月 20 日
The most obvious solution is to appreciate how a number is constructed in decimal form. So, rather than convert to characters, then concatenate, and then go back in to numeric form, simply use base 10.
C = 10*A + B;

  3 件のコメント

Guillaume
Guillaume 2017 年 2 月 20 日
And a generic version of that for integers of any (reasonable) magnitude:
C = A .* 10.^ceil(log10(B)) + B
John D'Errico
John D'Errico 2017 年 2 月 20 日
True. I expect that whenever I see an example with one digit integers, the real problem is always more complex. :)
Walter Roberson
Walter Roberson 2017 年 2 月 20 日
I would max(1,log10(max(abs(B))))
though the desired outcome is not clear for negative values.

サインイン to comment.

More Answers (1)

Adriano
Adriano 2017 年 2 月 21 日
Many thanks!!!!

  0 件のコメント

サインイン to comment.

サインイン してこの質問に回答します。


Translated by