0
votes

I am running a program where at each time loop I obtain a N dim. vector v(t).

At the end of the program, I want to obtain a vector (or array?) which contains vectors from each iteration that is created by taking each element of v(t) and subtracting it will all other elements. However I am only interested in the absolute difference of the elements.

Therefore, if I let u denote the vector with subtracted elements at some arbitrary iteration, I want for instance that u(1)(t_0) = v_1(t_0) - v_2(t_0) but I don't also need u(k) = v_2(t_0) - v_1(t_0) for some index k

At each iteration t I also create a time vector Time(end+1) = t.

At the end I obtain a vector of vectors: [u_1, u_2, u_3,...] and a time vector Time = [t_1 , t_2 , ...] and I want to plot [u_1(i), u_2(i) , ...] against Time for all i in the same plot.

Here is a small example:

T = 400
dt = 1e-1
u = [];
Time = [];
v = [0; 0; 0; 0;];

for t = 0:dt:T
v = v + func(t)
Y = subtractFunc(v) % I am looking for help to create this subtractFunc..
Time(end+1) = t;
u(end+1) = Y;
end 

% ... And help to create the properplot command, below code not appropriate
plot(u,Time)

To sum it up: How do I create subtractFunc (or a some inline command) that takes creates a new vector Y containing the absolute difference between each pair of elements in v?

And how do I then go about and plot all the differences in u at each time against the time vector Time?

1
Please add a small hand calculated example - Dan
Should be more readable now. - Fabric

1 Answers

0
votes

You can use MATLAB diff to take pairwise differences, then take the absolute value:

>> v = [1 3 6 -2 6];  %% random values
>> diff(v)
ans =
     2     3    -8     8
>> abs(diff(v))
ans =
     2     3     8     8