Compare pair of rows in a matrix
2 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Star Strider
on 25 Aug 2016
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!