Giải: Hàm tic() phải sử dụng trước hàm toc().
% So luong sinh viên
N = 10^7;
% Khoi tao bang diem
Diem = randi([0 10],1,N);
% Theo Matlab
tstart = tic;
NoP=0;
for idx = 1: length(Diem)
if Diem(idx) >= 5
NoP = NoP + 1;
end
end
disp(strcat('S luong sinh viên dau theo C=',num2str(NoP)));
disp(strcat('Thoi gian chay theo C = ',num2str(toc(tstart))));
% Theo Matlab
tstart = tic;
NoP = sum(Diem >= 5);
disp(strcat('So luong sinh viên dau theo Matlab =',num2str(NoP)));
disp(strcat('Thoi gian chay theo Matlab =',num2str(toc(tstart))));