Why does this appear as empty?

조회 수: 2 (최근 30일)
Seong Nam
Seong Nam 2017년 4월 25일
답변: Walter Roberson 2017년 4월 25일
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
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?
Seong Nam
Seong Nam 2017년 4월 25일
Yes, that's what I meant

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

답변 (2개)

Image Analyst
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

Walter Roberson
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)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by