Starting 'for' loop from integers (for e.g. 7 ) after 1 and not counting lower integers value (for e.g. 1-6)?

1 view (last 30 days)
I am starting a for loop as,
for I=7:12
end
and expect the results which are meant to be from 7 to 12. But I am also getting values (however they are zero) for I=1:6 as
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
316.2000 294.7000 279.1000 295.7000 298.5000 286.0000 252.6000 254.5000 276.2000 294.4000
485.0000 466.1000 463.2000 460.2000 489.2000 476.7000 396.5000 420.7000 443.8000 465.3000
509.9000 485.5000 486.3000 483.1000 483.6000 491.0000 414.2000 428.8000 470.8000 486.7000
517.6000 505.3000 508.5000 511.1000 501.1000 506.5000 442.7000 461.1000 505.6000 507.5000
473.8000 445.7000 455.7000 433.0000 455.6000 450.9000 367.2000 400.6000 422.5000 435.5000
481.4000 457.0000 457.5000 445.6000 459.5000 452.6000 389.8000 402.8000 431.7000 445.7000
How do I avoid zeroes?

Answers (1)

Christopher Madec
Christopher Madec on 25 Jul 2018
What do you want to have instead of zeros ? Matlab automatically assign a 0 for lower values if you not putting any value on them with your loop. You could potentially put Not an Number (NaN) values with:
for ii=1:6
A(ii)=NaN;
end
  2 Comments
Guillaume
Guillaume on 25 Jul 2018
@Raju,
You've left out what goes inside your loop leaving it up to us to guess what your problem is. My guess, is that you've written something like:
for i = 7:12
something(i, :) = somethingelse
end
In matlab matrix indices always start at 1. The first row of matrix is always index 1. It can't be an arbitrary number such as 7. Thus if you tell matlab to fill row 7 with something, it will also create row 1 to 6 and fill them 0.
Whether you want it or not, that's what matlab does. If you do not want these row of 0, then start filling from row 1, e.g.:
for i = 7:12
rowindex = i-6;
something(rowindex, :) = somethingelse;
end

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!