필터 지우기
필터 지우기

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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by