MATLAB: How to index so end+1 goes to the beginning of an array

arrayindex

I am trying to index an array so that the index [end +1] refrences the first value in that array rather than the index exceeding array bounds. Is there a simple way to do this in Matlab?

Best Answer

>> V = 1:4;
>> V(1+mod((end+1)-1,end))
ans =
1
>> V(1+mod((end+0)-1,end))
ans =
4
>> V(1+mod((end-1)-1,end))
ans =
3