This should work:
for userTouristicTrajidx = 1:numel(userTouristicTraj)
for touristicDataidx = 1:numel(userTouristicTraj(userTouristicTrajidx).touristicData)
touristicTraj = userTouristicTraj(userTouristicTrajidx).touristicData(touristicDataidx).touristicTraj2;
traj = nan(size(touristicTraj, 1), 1);
if ~isempty(touristicTraj)
[found, whichrow] = ismember(touristicTraj, Clusters(:, [1, 2]), 'rows');
traj(found) = Clusters(whichrow(found), 4);
end
userTouristicTraj(userTouristicTrajidx).touristicData(touristicDataidx).traj = traj;
end
end
Note: This is using ismember which means that the numbers must be exactly identical*. If the numbers in touristicTraj2 and Clusters were obtained from different calculations, then it is very likely they won't be exactly the same (e.g. 0.1+0.1+0.1 is not the same as 0.3). If that is the case, then use ismembertol instead. ismembertol is significantly slower than ismember.
Note 2: I don't know how you generated your touristicTraj2 fields but some of them are empty with size (0, 0). It would have been much better to create them empty with size (0, 2). While for most purpose both are the same (just empty arrays), it matters for ismember. Using the latter empty would have avoided the if ~isempty(touristicTraj) test.
Note 3: You haven't specified what to write when no match is found. I've put NaN. Replace as appropriate.
Best Answer