Filter löschen
Filter löschen

Compare pair of rows in a matrix

2 Ansichten (letzte 30 Tage)
Neelabh Pant
Neelabh Pant am 25 Aug. 2016
Kommentiert: Star Strider am 25 Aug. 2016
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.

Antworten (1)

Star Strider
Star Strider am 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 Kommentare
Neelabh Pant
Neelabh Pant am 25 Aug. 2016
This makes sense. Thank you.
Star Strider
Star Strider am 25 Aug. 2016
My pleasure.
If it solves your problem, please Accept it.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by