MATLAB: Logical Indexing a spefic column in a matrix

indexing matrix

Hi,
I have a matrix with 5 columns and a variable number of rows (usually around 100). Column 4 of the matrix is a set of angles that can range anywhere from -180 to 180. I want to be able to create a set of smaller matrixes that splits up the larger matrix based on whether the angles fall in a 30 degree segment or not. For example a matrix that has 0 to 30 degrees, 30 to 60 degrees, 60 to 90 degrees, etc.
Can I use something like below. I don't think that will work though.
Zero_to_Thirty = A(4,:) < 30
Any help is appreciated!
Thanks,
Pat

Best Answer

Don't store these submatrices in individual variables with different names.Rather store them in a cell array.
Use histcounts to find the distribution of the indices of the rows you want to split and arrayfun or a for loop to do the splitting:
m = rand(150, 6); m(:, 4) = randi([-180 180], 150, 1); %random matrix for demo, size 150*6
[~, ~, bins] = histcounts(m(:, 4), -180:30:180);
subm = arrayfun(@(bin) m(bins == bin, :), 1:max(bins), 'UniformOutput', false)
That last line with the arrayfun is equivalent to
subm = cell(1, max(bins));
for bin = 1:max(bins)
subm{bin} = m(bins == bin, :);
end