Use for loop to display the following numbers in sequence separated by “,”
1, 2, 3, 4, 5, 6, 7, 8, 9, 10
this is what I have so far but I can't figure out how to get rid of the coma after the 10.
A1=' ';
for i=1:1:10
A2=[num2str(i),','];
A1=[A1,A2];
end
disp(A1)

 채택된 답변

Henrik
Henrik 2014년 10월 18일

0 개 추천

A1=' ';
for i=1:1:10
if i<10
A2=[num2str(i),','];
else
A2=num2str(i);
end
A1=[A1,A2];
end
disp(A1)
You can probably do it more efficiently than this, though..

댓글 수: 1

Stephen
Stephen 2014년 10월 18일
Thankyou been banging my head on this for the last four days.

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 18일

0 개 추천

Try this:
m = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
str = sprintf('%d, ', m);
fprintf('\n%s\n', str(1:end-2));

댓글 수: 2

Stephen
Stephen 2014년 10월 18일
This is a question from an introductory Matlab course at my local college and I need to use a for loop to produce this.
A1=' ';
for i=1:1:10
if i <= 9
A2=[num2str(i),','];
else
A2 = num2str(i);
end
A1=[A1,A2];
end
disp(A1)

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

태그

질문:

2014년 10월 18일

댓글:

2014년 10월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by