How to Speed up code
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I have this code: I have a number of user and for all of them I want to find the id of locations that they have visited, the stay time and, combining this elements,the semantic trajectories associated to them
for userid=1:usercount
% identification locationId: the location id are in a struct s.loc_ids
for i=1:size(Trajectories(1,userid).label,1)
Trajectories(1,userid).locationId(i,1)=s(1,k).loc_ids(i,1);
end
for i=1:size(Trajectories(1,userid).label,1)-1
% stayTime (difference between time of arrival and time of exit from a cell) for every user
Trajectories(1,userid).stayTime(i,:)=abs((Trajectories(1,userid).dateAll(i+1,:)-Trajectories(1,userid).dateAll(i,:)));
% stayLocation for every user
Trajectories(1,userid).stayLocation(i,:)=[Trajectories(1,userid).locationId(i,1) Trajectories(1,userid).dateAll(i+1,:) Trajectories(1,userid).stayTime(i,:)];
% semantic trajectories for every user
Trajectories(1,userid).semanticTraj(i,:)=[Trajectories(1,userid).stayLocation(i,:) Trajectories(1,userid).label(i,:)];
end
end
It run and do what I want but It's slow: can you help me to obtain better performance?
댓글 수: 0
채택된 답변
Jan
2016년 5월 16일
편집: Jan
2016년 5월 16일
for userid = 1:usercount
T = Trajectories(1, userid); % Nicer code...
n = size(T.label,1);
T.locationId(1:n) = s(1,k).loc_ids(1:n);
% Or perhaps:
% T.locationId = s(1,k).loc_ids;
T.stayTime = abs(diff(T.dateAll - T.dateAll, 1, 1));
T.stayLocation = [T.locationId(1:n-1), T.dateAll(2:n, :), T.stayTime(1:n-1, :)];
T.semanticTraj = [T.stayLocation(1:n-1,:), T.label(1:n-1, :)];
Trajectories(1, userid) = T;
end
Without your data, I cannot debug this. Perhaps you need some .' operators for transposing.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!