MATLAB: I am having problems saving the result of a for loop to a new array

for loop array outputMATLAB

I am very new to matlab (started yesterday), so I might be doing something very obviously wrong.
I am trying to work through increments of 0.1 to 4 in 0.1 steps, and count the number of results in another array (called 'p_11_10') that are less than each increment, and then output these results to an array.
When I run the code without trying to output to an array I can see the calculation is working correctly, but when I put the last line in, I get the error:
'Index in position 1 is invalid. Array indices must be positive integers or logical values'
for i = increments
y = (sum(p_11_10 < i)/total_n)*100
m(i,:) = y
end
increments is defined earlier in the code (because we might want to change it) as:
low_thick = 0.1;
high_thick = 4.0;
incre_thick = 0.1;
increments = low_thick:incre_thick:high_thick;
I watched some videos and read some of the responses on this website with similar looking problems, and that is where the 3rd line of the top block comes from.
I'm guessing this is something to do with how I'm indexing through the loop, but I'm not sure.
Any help would be much appreciated.

Best Answer

You can modify your loop slightly to save it a bit differently to your matrix. Be careful with using i as loop index: it can cause bugs when it is interpreted as the imaginary unit. Avoid j for the same reason. Pre-allocation of output matrices can improve execution speed, especially for long loops.
low_thick = 0.1;
high_thick = 4.0;
incre_thick = 0.1;
increments = low_thick:incre_thick:high_thick;
m=zeros(numel(increments), ## number of cols ## );
for ind = 1:numel(increments)
y = (sum(p_11_10 < increments(ind))/total_n)*100
m(ind,:) = y;
end
@madhan, Loop increments can be any value, but if you plan on using them on using them as an index to matrix you should make them positive integers.