¿Restar cada elemento de un vector del otro en matlab? [cerrado]

I have a column vector, for example,

1

0

3

2

At each row, i want to subtract the number above it, like,

row1: null

row2: 0 - 1 = -1

row3: 3 - 0 = 3 and 3 - 1 = 2 .. etc so that to obtain something like this

nulo

-1

3 2

-1 2 1

Can someone guide me to an efficient MATLAB code without using a for loop, as the number of rows in my original data is too long.

Gracias de antemano.

preguntado el 27 de noviembre de 13 a las 05:11

1 Respuestas

Podrías hacer algo como:

V = [1;0;3;2];
tril(bsxfun(@minus,V,V'),-1);

Esto me da lo siguiente:

ans =

 0     0     0     0
-1     0     0     0
 2     3     0     0
 1     2    -1     0

The main downside of this is that it will use a lot of memory if V is very long (but that is going to be a problem regardless, I imagine). You can potentially reduce the memory usage by making V escaso.

respondido 27 nov., 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.