Borrar filtros
Borrar filtros

matrix dimension error: need some hint!

1 visualización (últimos 30 días)
Charles Martineau
Charles Martineau el 26 de Mayo de 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!

Respuesta aceptada

Walter Roberson
Walter Roberson el 26 de Mayo de 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 comentario
Image Analyst
Image Analyst el 26 de Mayo de 2012
Also note that Xt(2:end)-Xt(1:end-1) is the same as diff(Xt).

Iniciar sesión para comentar.

Más respuestas (1)

Charles Martineau
Charles Martineau el 26 de Mayo de 2012
Superb! Thanks a lot!!!

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by