Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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!!

Réponse acceptée

Charles Martineau
Charles Martineau le 30 Mai 2012
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;

Plus de réponses (3)

Walter Roberson
Walter Roberson le 27 Mai 2012
Xt(j:end-j+1) is not the same size as Xt(1:end-j)

Image Analyst
Image Analyst le 27 Mai 2012
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 le 27 Mai 2012
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 commentaire
Image Analyst
Image Analyst le 27 Mai 2012
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)?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by