MATLAB: Writing a loop “for”

matlab loop for writing problem

Hey, i have a problem with writing a loop, which will be able to do this:
phi(1)=a_locs(1)/((a_odd(1)));
phi(2)=a_locs(2)/((a_odd(1)));
phi(3)=a_locs(3)/((a_odd(1)));
phi(4)=a_locs(4)/((a_odd(1)));
phi(5)=(a_locs(5)-a_odd(1))/((a_odd(2)));
phi(6)=(a_locs(6)-a_odd(1))/((a_odd(2)));
phi(7)=(a_locs(7)-a_odd(1))/((a_odd(2)));
phi(8)=(a_locs(8)-a_odd(1))/((a_odd(2)));
phi(9)=(a_locs(10)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
phi(10)=(a_locs(11)-(a_odd(1)+a_odd(2)))/((a_odd(3)));
Someone can help me?

Best Answer

With a loop:
phi = zeros(1, 452); % Instead of 450?
s = 0;
m = 1;
for k = 1:4:452
phi(k:k+4) = (a_locs(k:k+4) - s) / a_odd(m);
s = s + a_odd(m);
m = m + 1;
end
And vectorized - assuming that a_odd is a row vector:
phi = reshape(a_locs, 4, 113);
phi = bsxfun(@minus, phi, [0, cumsum(a_odd(1:112)]);
phi = bsxfun(@rdivide, phi, a_odd(1:113));
phi = reshape(phi, 1, 452);