Hello everyone,
At first, I had the following code:
for ii = 1:numel(data.classes) switch data.classes{ii} case 1 data.classes{ii} = 'case 1'; % Active classes
case 2 data.classes{ii} = 'case2'; otherwise disp('Invalid case.'); endend
However, I know that vectorized code is preferred instead of loops, so I changed it to
case1Found = ismember(data.classes, case1Members);case2Found = ismember(data.classes, case2Members);data_.classes(case1Found) = {'case1'};data_.classes(case2Found) = {'case2'};
When comparing their performance (execution time) I was surprised to see that the first option, with loops was twice as fast than the vectorized option (0.014688 s vs. 0.029204 s)!
Why is this? Thanks 😉 !
Best Answer