MATLAB: What’s wrong with the formula? – Can’t solve the matrix dimension issue…

dimensionsfor loop

Hi everyone, My objective is the following: – to make this equation in matlab: see equation (a partition function) here:
some information on the equation: * q can take any value between 1 to 5 – for now I assume 2. * X(i delta t) is the change in price for interval i and X(i deltat t + deltat t ) is the change in price in the following interval.
I start by creating my vector Xt which is of size 12588 X 1. Think of each element of the vector as a price of a stock for each day.
My final result should be a vector where each element is the result of the above formula for different size of interval.
This is my current formula so far in matlab:
for j=2:126,
S(j-1)=sum((abs((Xt(j+1:end)-Xt(j:end-j+1))-(Xt(j:end-j+1)-Xt(1:end-j)))).^2);
end;
Error using -
Matrix dimensions must agree.
j=2:126 is the various interval in the change of price that I wish to compute.
If someone can help me out with my problem… please! I've been struggling on this one for a while now! Thank you!!

Best Answer

the correct way:
for j=1:125;
E=D(1:j:end,j);
EE=diff(E(2:end));
EEE=diff(E(1:end-1));
S(j)=sum(abs(EE-EEE).^2);
end;