Here’s a fun function that I wrote. I call it snipIndex.

```function cell_out = snipIndex(arr)
% snipIndex
%   Input: arr - one dimensional logical array
%
%   Output: cell_out - a cell array
%
%   This function takes in a 1D logical array and outputs a cell array that
%   finds the indices of arr. If arr was a 1xN logical array, used to index
%   into another 1xN array, then each cell of cell_out can be used to also
%   index into that 1xN array.

% Initialization
cell_out = cell(1);
cell_out_iter = 1;
prev_val = arr(1);
prev_ind = 1;

for ii = 2:length(arr)

% Continue if the previous value is equal to the current value, i.e.
% this point will only be passed if the logical array arr changes from
% 1 to 0 or from 0 to 1
if prev_val == arr(ii)
continue
end

% If the previous value was 0, then we have entered a string of ones,
% set prev_val and prev_ind
if ~prev_val

prev_val = 1;
prev_ind = ii;

% Otherwise, the previous value was 1, so write out the indices to
% cell_out. This statement can also be reached if the prev_ind was the
% first index of arr. This second way of getting into this statement
% looks for a string of ones at the start of arr.
elseif prev_val || prev_ind == 1

cell_out{cell_out_iter} = prev_ind:ii-1;
prev_val = 0;
prev_ind = ii;
cell_out_iter = cell_out_iter + 1;

end
end

% This final check looks for a string of ones at the end of arr.
if arr(end)
cell_out{cell_out_iter} = prev_ind:length(arr);
end

end```

What does it do? Well, I think it’s best observed through an example.

Take the following logical array, that is, an array of only 1’s and 0’s.

`[1 1 1 0 0 1 1 0 0 1 1 1]`

This array is of size 12×1, so you can use it in MATLAB to index into any 12×1 array. But sometimes you just want the values to be split up into segments. That’s where snipIndex comes into play. If you run snipIndex on the above logical array, the output would be:

`{ {1 2 3}, {6 7}, {10 11 12} }`

snipIndex splits up the logical array into chunks, based upon consecutive 1’s from the logical array. How is this useful? Take the following plot.

It’s basically two sinusoids overlapping. snipIndex can be used to isolate the indices where one sinusoid is greater than another. The isolation is key. By isolating the indices, we can do cool things like coloring in the area where one is larger than the other, like this: