How can I accomplish the following?
1 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
Henrik
2014-10-18
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 个)
Image Analyst
2014-10-18
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 个评论
Image Analyst
2014-10-18
A1=' ';
for i=1:1:10
if i <= 9
A2=[num2str(i),','];
else
A2 = num2str(i);
end
A1=[A1,A2];
end
disp(A1)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!