How to Speed up code

1 次查看(过去 30 天)
elisa ewin
elisa ewin 2016-5-16
编辑: Jan 2016-5-16
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?

采纳的回答

Jan
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 个)

类别

Help CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by