Why does this appear as empty?
1 次查看(过去 30 天)
显示 更早的评论
I want to create a from 200 to -300 with the direction going down by 2. I also want to skip the numbers from 197 to -298 with some dots. So it would look something like this:
I =
200
198
.
.
.
.
-298
-300
I understand I first have to start with I:(200:-300)' But it always come up as 0×1 empty double column vector
I also can't remember how the numbers can be counted by 2 AND skip from 198 to -298. Any help would be appreciated
2 个评论
James Tursa
2017-4-25
Do you mean you are trying to display the vector this way? Even though it contains all of the intermediate numbers? Or what?
回答(2 个)
Image Analyst
2017-4-25
You say "I want to create a" but then you later create and use I. You say "skip the numbers from 197 to -298" but then your expected results actually include -298. Anyway, ignoring those discrepancies, I came up with this:
a = (200 : -2 : -300)'; % Or use I instead of a.
for k = 1 : length(a)
if a(k) <= 197 && a(k) > -298 % or >= -298
fprintf('.\n');
else
fprintf('%d\n', a(k));
end
end
0 个评论
Walter Roberson
2017-4-25
The basic issue is that when you have A:B and B < A, then the result will always be empty. The default increment is always 1. If you want to decrement, then you need to specify a negative increment, such as 200:-2:-300 .
The alternative is to use
fliplr(-300:2:200)
0 个评论
另请参阅
类别
在 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!