Logical Indexing a spefic column in a 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

 Réponse acceptée

Guillaume
Guillaume le 17 Déc 2014
Modifié(e) : Guillaume le 17 Déc 2014
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

5 commentaires

Patrick's comment copied here: Thanks for the help but I'm using 2012, which I don't think has histcounts. Is there an m file for that function?
Use histc instead, that's what histcount replaces:
[~, bins] = histc(m(:, 4), -180:30:180);
Note that the behaviour of histc is different from histcounts with regards to the last bin. With histcounts, 180 is part of the [150 180] bin, with histc, it is its own bin.
Patrick
Patrick le 17 Déc 2014
Guillaume,
Thanks for the help. I got histc a cell array. Now I'm kind of confused on what to do with that array. I'll read up on cell arrays and see if I can figure it out. What I eventually want to do is to be able to find the largest value in column 5 for each array and assign that to a variable. And if an array is empty, to look to the range before or after to see which one has a largest value in column 5 (For example if 0 to 30 is empty, look in the ranges 30 to 60 and -180 to -150 and assign the variable to the largest value of those two arrays, and keep repeating all the way around the circle if there are multiple empty arrays).
If you have any suggestions to the above that'd be awesome but I appreciate all your help so far.
If you put the result of the split in individual variables, then it becomes difficult to apply the same algorithm to each one of them. With a cell array, it's much simpler, so I would recommend you do read up on them.
To get the max of column 5 in each sub matrix and substitute with the last max if empty:
max_subm = zeros(size(subm))
lastmax = -Inf;
for idx = 1:numel(subm)
if ~isempty(subm{idx})
lastmax = max(subm{idx}(:, 5));
end
max_subm(idx) = lastmax;
end
Patrick
Patrick le 18 Déc 2014
Ok this works fantastic. Except I forgot to mention that I actually needed the values in column 1 of the matrix in the subm cell array, that correspond to the largest values in column 5. Is there a way to keep them together?
All the columns are present in the matrices of the subm cell array, so I'm not sure what you're asking.
If you want the value of column 1 that correspond to the maximum, use the second return value of max:
max_subm = zeros(numel(subm), 2)
lastmax = -Inf;
lastval = -Inf;
for idx = 1:numel(subm)
if ~isempty(subm{idx})
[lastmax row] = max(subm{idx}(:, 5));
lastval = subm{idx}(row, 1);
end
max_subm(idx, :) = [lastmax lastval];
end

Connectez-vous pour commenter.

Plus de réponses (1)

If M is the original matrix,
c4 = M(:,4);
M1 = M(0<=c4 & c4<30,:); % <-- Having the angles between 0 and 30
M2 = M(30<=c4 & c4<60,:); % <-- Having the angles between 30 and 60
etc.

2 commentaires

Stephen23
Stephen23 le 18 Déc 2014
Surely it would be best not to split the array up into numbered variables. Doing this seems to invite the next question "how do I evaluate sequentially-numbered variable names?" which inevitably leads on to eval...
Roger Stafford
Roger Stafford le 18 Déc 2014
Not necessarily. It depends on how many there are and how they are to be used.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by