MATLAB: Label segments based on percentage

celllabel;labellinglengthMATLABmatrixpercentagesegmentvariable

Hi,
I have a variable (AL_128 which I have attached). It is a 1×48 cell where each cell is a Ax20 matrix. The matrices contain the values 0 and 1. For instance, cell 1 is a 225×20 matrix which should be interpret as follows: 225 segments where each segment is of length 20.
What I want to do here is to say that if 30% of the segments contain the label 1, the whole segment should be labelled 1. Therefore, cell 1 will end up being a 225×1 vector. This should be done for all segments and all cells.
How can I do that?

Best Answer

Much simpler code:
threshold = 0.3; %30% threshold
M = cellfun(@(m) mean(m, 1) >= threshold, AL_128, 'UniformOutput', false)
Or if you really want to use a loop over the cell array:
M = cell(size(AL_128));
for cidx = 1:numel(AL_128)
M{cidx} = mean(AL_128{cidx}, 1) >= threshold;
end
There is certainly no need to loop over the columns of the matrix.