How to concatenate an array of numbers without for loop

1 Ansicht (letzte 30 Tage)
JFz
JFz am 22 Mär. 2018
Kommentiert: JFz am 22 Mär. 2018
Hi, I have an array of integer numbers A = [1, 2, 3]. I would like to concatenate the numbers for use in SQL statement like '(1, 2, 3)'. How to do that without a for loop? Thanks.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Mär. 2018
>> A = [1,2,3];
>> str = sprintf(', %d',A);
>> str = sprintf('(%s)',str(3:end))
str = (1, 2, 3)

Weitere Antworten (1)

Guillaume
Guillaume am 22 Mär. 2018
A = [1 2 3];
sprintf('(%s)', strjoin(compose('%d', A), ', '))
  3 Kommentare
Guillaume
Guillaume am 22 Mär. 2018
compose was introduced in version R2016b. (You should say in your question if you're several versions behind).
In earlier versions, you can replace compose by the undocumented sprintfc:
sprintf('(%s)', strjoin(sprintfc('%d', A), ', '));
Or use Stephen's method of using sprintf then removing the initial ', '.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu System Composer finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by