I am not certain this is more efficient than your code, however it takes advantage of the repeating patterns to make it simpler:
a = randi(9, 6, 9);
C1 = 4 : 4 : 32;
ahatfcn = @(k) [a(1,k),a(2,k),a(3,k),a(4,k),a(5,k),a(6,k)];
for ii = 1 : 40
if ii < 5
ahat = ahatfcn(1);
else
for k1 = 1:numel(C1)
if ((C1(k1)<ii) && (ii<(C1(k1)+5)))
ahat = ahatfcn(k1+1);
end
end
end
end
a = ahat
It runs without error. I defer to you to determine if it produces the result you want.
Best Answer