Filter löschen
Filter löschen

matrix dimension error: need some hint!

1 Ansicht (letzte 30 Tage)
Charles Martineau
Charles Martineau am 26 Mai 2012
Hi,
I have a vector "Xt" where Xt dimensions are 12588 X 1. From this vector I apply this code:
S=sum((abs(Xt(2:end)-Xt(1:end-1))).^2);
which result in one number where S is a 1by1.
Now my objective is to construct a vector S (Nx1) where each value in S depends on J and K -- for instance:
S=sum((abs(Xt(j:end)-Xt(k:end-1))).^2);
At first I thought of the following (I don't want J do exceed 126):
for j=2:126 k=1:125,
S=sum((abs(Xt(j:end)-Xt(1:end-k))).^2);
end;
I got this: "Error using - Matrix dimensions must agree."
Any insight at how I can correct this matrix issue?
Thank you!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 26 Mai 2012
Your line
for j=2:126 k=1:125,
is equivalent to
for j=2:126;
k=1:125
That is, assign k the vector 1:125 each time through the "for j" loop.
I get the impression that you may have been trying to loop over j and k. If so then you need "for" statements for each of the loops.
If you were to correct that, then you have the problem that Xt(j:end) is not going to be the same length as Xt(1:end-k) so you will not be able to subtract the two vectors.
Also, you overwrite "S" each time through the loop, which you probably do not want to do.
Perhaps you want something like
for j=2:126
S(j-1) = sum((abs(Xt(j:end)-Xt(1:end-j+1))).^2);
end
If that is the case, then
S = fliplr( cumsum( fliplr( (Xt(2:end)-Xt(1:end-1)).^2 ) ) );
Note that unless you are working with complex numbers, you do not need to abs() numbers before squaring them.
  1 Kommentar
Image Analyst
Image Analyst am 26 Mai 2012
Also note that Xt(2:end)-Xt(1:end-1) is the same as diff(Xt).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Charles Martineau
Charles Martineau am 26 Mai 2012
Superb! Thanks a lot!!!

Community Treasure Hunt

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

Start Hunting!

Translated by