Assuming size_of_array < n, to iterate through array elements multiple times, normally I would use something like (in C++):
for (size_t i = 0; i < n; ++i)
{
elem = arr[ i % size_of_arr ];
}
If arr = {1, 2, 3};, for elem I would get:
1 2 3 1 2 3 ...
However, in MATLAB / Octave indexes start from 1 and:
for i = 1 : n
elem = arr( mod( i, length(arr) + 1) );
end
so when n == length(arr) I get an error:
error: arr(0): subscripts must be either integers 1 to (2^31)-1 or logicals
How is this done when indexes start from 1?
mod(i-1,n)+1(I call it 1-based modulus). So, perhapselem = arr(mod(i-1, length(arr))+1);works for you (I'm not sure what you are trying to achieve)length, it returns the length of the longest dimension. Instead, usenumel(number of elements).error: arr(0):The zero is what Matlab is trying to yell at you about.