Compare pair of rows in a matrix

조회 수: 1 (최근 30일)
Neelabh Pant
Neelabh Pant 2016년 8월 25일
댓글: Star Strider 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
Neelabh Pant
Neelabh Pant 2016년 8월 25일
This makes sense. Thank you.
Star Strider
Star Strider 2016년 8월 25일
My pleasure.
If it solves your problem, please Accept it.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by