필터 지우기
필터 지우기

How to concatenate an array of numbers without for loop

조회 수: 3 (최근 30일)
JFz
JFz 2018년 3월 22일
댓글: JFz 2018년 3월 22일
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.

채택된 답변

Stephen23
Stephen23 2018년 3월 22일
>> A = [1,2,3];
>> str = sprintf(', %d',A);
>> str = sprintf('(%s)',str(3:end))
str = (1, 2, 3)

추가 답변 (1개)

Guillaume
Guillaume 2018년 3월 22일
A = [1 2 3];
sprintf('(%s)', strjoin(compose('%d', A), ', '))
  댓글 수: 3
Guillaume
Guillaume 2018년 3월 22일
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 ', '.
JFz
JFz 2018년 3월 22일
Thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 System Composer에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by