Compare pair of rows in a matrix

1 次查看(过去 30 天)
Neelabh Pant
Neelabh Pant 2016-8-25
Suppose I have a Matrix like A = [12.05; 12.10; 12.25; 12.45] and I want to find difference between A(1) and A(2) then A(2) and A(3) and so on. Basically I am trying to compare time column and I have a huge data set with maybe 900,000 rows. I used a simple for loop which seems to take a very long time (naturally). Can you help me optimize this issue. Here's my code.
for j = 1:(length(usr)-1)
t11 = usr(j,5);
t22 = usr(j+1,5);
secs = fix((datenum(t22) - datenum(t11)) * 86400);
if (secs > 600 && secs < 1800)
placesLat = [placesLat;usr((j+1),2)];
placesLong = [placesLong;usr((j+1),3)];
end
end
Where placesLat and placesLong are just an array. I cannot assign memory before hand because I am not sure about the final result length.
Thanks in advance.

回答(1 个)

Star Strider
Star Strider 2016-8-25
There are two possibilities, diff and filter:
A = [12.05; 12.10; 12.25; 12.45];
dA = filter([1 -1], 1, A); % Use ‘filter’
dA = diff(A); % Use ‘diff’
The advantage to using filter is that the resulting vectors are the same lengths. The diff output is one element shorter than the input.
  2 个评论
Star Strider
Star Strider 2016-8-25
My pleasure.
If it solves your problem, please Accept it.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by