sprintf: how to efficiently create a string of 75 numbers, separated by comma
이전 댓글 표시
I have an array which contains 75 numeric elements. I want to write these numbers as a string separated by a comma. If there were few numbers, I could use 'sprintf' as follows:
A=[1,2];
str=sprintf('%d,%d',A(1:end));
I need an efficient way to do it when there are many entries in A.
채택된 답변
추가 답변 (2개)
Ameer Hamza
2018년 4월 27일
편집: Ameer Hamza
2018년 4월 27일
This will work.
strjoin(string(A), ',')
댓글 수: 2
Walter Roberson
2018년 4월 27일
Note: this requires R2016b or later. Also, it creates a string object output, not a character vector.
Ameer Hamza
2018년 4월 27일
Thanks for elaborating.
"I need an efficient way to do it when there are many entries in A."
This is probably about the most efficient way:
A = 1:5;
str = sprintf(',%d',A);
str = str(2:end);
Timing comparisons for 1e4 iterations:
Elapsed time is 0.539053 seconds. % this answer.
Elapsed time is 2.54425 seconds. % Star Strider's answer.
카테고리
도움말 센터 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!