What's wrong with my formula? - Can't solve my matrix dimension issue...

1 回表示 (過去 30 日間)
Charles Martineau
Charles Martineau 2012 年 5 月 27 日
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!!

採用された回答

Charles Martineau
Charles Martineau 2012 年 5 月 30 日
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;

その他の回答 (3 件)

Walter Roberson
Walter Roberson 2012 年 5 月 27 日
Xt(j:end-j+1) is not the same size as Xt(1:end-j)

Image Analyst
Image Analyst 2012 年 5 月 27 日
What I do in situations like this is to not try to cram so many things into one expression. If you make up some intermediate variables you will quickly see the problem:
Xt = rand(12588, 1);
for j=2:126
x1 = Xt(j+1:end);
x2 = Xt(j:end-j+1);
v1 = x1 - x2;
v2 = Xt(j:end-j+1)-Xt(1:end-j);
S(j-1)=sum(abs(v1-v2).^2);
end
Note how x1 is 12585 elements long and x2 is 12584 elements long. Thus you can't subtract them.

Charles Martineau
Charles Martineau 2012 年 5 月 27 日
Thanks for the insights... ya that's what I was expecting that my two vectors will not have the same size. Yet, those anyone know how I can replicate the above formula in matlab? I think it might be because of the value j takes... because I do the following:
for j=2:2
x1 = Xt(j+1:end);
x2 = Xt(j:end-j+1);
v1 = x1 - x2;
v2 = Xt(j:end-j+1)-Xt(1:end-j);
S(j-1)=sum(abs(v1-v2).^2);
end;
...it works...it creates the first element of my "wanted" vector.
  1 件のコメント
Image Analyst
Image Analyst 2012 年 5 月 27 日
Yes, it will work in the special case of using ONLY 2 but not in the general case. I'm not sure why you were expecting different sized vectors and why you thought that would work. And I'm not really sure what X(i deltat t + deltat t ) means. I'm not familiar with that terminology. Would that be the same as X(i * deltat + deltat)?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by