フィルターのクリア

matrix dimension error: need some hint!

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 5 月 26 日
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 件のコメント
Image Analyst
Image Analyst 2012 年 5 月 26 日
Also note that Xt(2:end)-Xt(1:end-1) is the same as diff(Xt).

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

その他の回答 (1 件)

Charles Martineau
Charles Martineau 2012 年 5 月 26 日
Superb! Thanks a lot!!!

カテゴリ

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