Why does this appear as empty?

2 visualizaciones (últimos 30 días)
Seong Nam
Seong Nam el 25 de Abr. de 2017
Respondida: Walter Roberson el 25 de Abr. de 2017
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 comentarios
James Tursa
James Tursa el 25 de Abr. de 2017
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 el 25 de Abr. de 2017
Yes, that's what I meant

Iniciar sesión para comentar.

Respuestas (2)

Image Analyst
Image Analyst el 25 de Abr. de 2017
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 el 25 de Abr. de 2017
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)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by