Borrar filtros
Borrar filtros

Question about loop ranges

4 visualizaciones (últimos 30 días)
Kevin Shannon
Kevin Shannon el 24 de Feb. de 2015
Comentada: Image Analyst el 24 de Feb. de 2015
Hello all,
I am having an issue with MATLAB loops.
I have two codes written (they are too big to post so I am not going to). Both codes are identical EXCEPT for the loop ranges i.e. For i = 1 : 30 : 2000 and For i = 1 : 30 : 4000. Also I have IF statements that have different ranges in each code, the first one calculates values every 12 iterations up to the 2000 mark and the second takes value every 12 iterations up to the 4000 mark.
The code using the 1 to 2000 range works perfectly fine, no issues at all. The second that goes up to 4000 does not. I repeat that what is inside the loops are the exact same down to the comments.
Inside the loops I am calculating values and passing them to pre-allocated arrays (using zeros(x,y)) then at the end I remove the excess zero values, using x(x==0) = [], and plot what is in those arrays against the FOR loop range.
For the lower range of iterations I get exactly the same amount of data points in each array (for the X and Y axis) but when I simply increase the range I get a varied result, none of which are equal to the X axis.
Can anyone think of why simply changing the range for the loops would cause any error at all if what is inside them is identical?
I will also add that computer memory is not an issue, the codes use about 900 MB and I have access to 16 GB.
  1 comentario
Image Analyst
Image Analyst el 24 de Feb. de 2015
Can you boil down the problem to a smaller script that you CAN attach that demonstrates the problem?
And I don't know what "calculates values every 12 iterations up to the 2000 mark" means. Every 12 iterations? So like 1-12, then 13-24, then 25 - 36, etc. up to 2000? Well isn't that all iterations up to 2000? Or did you mean "every 12th iteration up to" meaning only on iterations #12, 24, 26, 48, etc. Note, the wording is slight but different in those two cases, but makes drastic difference in the meaning of the two statements.

Iniciar sesión para comentar.

Respuestas (2)

James Tursa
James Tursa el 24 de Feb. de 2015
The obvious guess would be that some of your results are exactly 0 when you get beyond 2000, so the method of cropping the zero values, x(x==0)=[], is removing more values than is left in the for range (1:30:4000).

Al-Motasem Aldaoudeyeh
Al-Motasem Aldaoudeyeh el 24 de Feb. de 2015
Editada: Al-Motasem Aldaoudeyeh el 24 de Feb. de 2015
When you use a colon to create a vector using start:increment:end command, the number of elements in the vector is given as: (end - start) / increment + 1. If, however, the number is not a pure integer , it will be rounded towards negative infinity which is the reason why you occasionally have more or less elements than you originally anticipate. Consider, for example, a vector given as 1:3:10 which yields a length of 4 (4 elements), but the vector 1:3:20 yields 7 elements which is not an intuitive result.
To solve this problem, you can simply enforce specific number of elements in the vector by using:
linspace(start, end, N)
where N is the number of vector elements. By using this function instead of a colon, MATLAB will choose appropriate increment value so that the number of elements is equal to that demanded by yourself

Categorías

Más información sobre Loops and Conditional Statements 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