# Subtracting values in an array between all the elements one by one

6 views (last 30 days)
MichaelO on 30 Jun 2020
Commented: MichaelO on 7 Jul 2020
Hi y'all!
I have to substract values from an array of 2192 days (lines) and 1000 values (columns) one by one. size(Qref) = 2192 x 1000. For example. Qref(x1,y1) - Qref(x1,y2); Qref(x1,y1) - Qref(x1,y3); Qref(x1,y1) - Qref(x1,y4)...... then, Qref(x1,y2) - Qref(x1,y3), Qref(x1,y2) - (x1,y4), (x1,y2) - (x1,y5), ....and so on until Qref(x2192,y999) - Qref(x2192,y1000). I have to run that 14th times by 5 cuz it is for a 14th days lead time and 5 bassins. I wrote the following code but takes so much time (almost 6 days) so; if someone can help me to make a simpler code I will really appreciate it.
% Qref is generated 14 times for 5. So, once this loop is finished it is going to be repeated 13 times more, then 5 times
Qref = rand(2192,1000)
for idays = 1:2192
for ii = 1:1000-1
for ik = ii+1:1000
D0(idays,ik,ii) = abs(Qref(idays,ii)-Qref(idays,ik));
end
end
end

madhan ravi on 30 Jun 2020
[X, Y] = meshgrid(x,y);
XY = [X(:), Y(:)];
Wanted = -diff(XY)

#### 1 Comment

MichaelO on 7 Jul 2020
Thank you so much, that helps me alot!