%% Shaker sort (SL25)
clear; close all; clc;
R = 10*rand(1, 30);
figure(1);
for startMove = (length(R)-1):-1:length(R)/2
   for pos = startMove:-1:(length(R)-startMove)
      if R(pos) > R(pos+1)
         R([pos, pos+1]) = [R(pos+1) R(pos)];
      end
   end
   for pos = (length(R)-startMove+1):startMove
      if R(pos) > R(pos+1)
         R([pos+1, pos]) = [R(pos), R(pos+1)];
      end
   end
   plot(R, '*', 'LineWidth', 2);
   pause(0.15);
end

%% Find within a vector (SL27)
v  = reshape(magic(4)', [1 numel(magic(4))])
v  = sort(v, 'descend');
v1 = mod(v, 3) == 0 & v > 10
v(v1)
find(v1)

%% Find within a matrix (SL28)
w50 = w(w < 50);
w50(end-2:end)
% w50 = [20; 12; 31.5]
[rw, cl] = find(w < 50, 3, 'last');

%% Decrypting a message (SL29)
chars = 'acdgmrs'; volts = 1:7;
message = blanks(length(samples));
for iVolt = volts
   logCondition = samples > (iVolt - 0.5) & ...
      samples < (iVolt + 0.5);
   indices = find(logCondition);
   message(indices) = chars(iVolt);
end
disp(message)

%% Accumarray example (SL33)
M   = accumarray(dta(:, 1), dta(:, 2))
acc =  K*M  % = -3576
% OR (equivalent to dot product):
% acc =  dot(K, M)
% acc =  sum(K.*M.')

%% Measured data (SL40)
data = accumarray(TimeAndPlace, MeasuredData, [], @mean);
dataA = data(:,1);
dataB = data(:,2);
dataC = data(:,3);
% acc =  sum(K.'.*M)