필터 지우기
필터 지우기

Label segments based on percentage

조회 수: 1 (최근 30일)
Uerm
Uerm 2019년 12월 12일
댓글: Uerm 2019년 12월 23일
Hi,
I have a variable (AL_128 which I have attached). It is a 1x48 cell where each cell is a Ax20 matrix. The matrices contain the values 0 and 1. For instance, cell 1 is a 225x20 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 225x1 vector. This should be done for all segments and all cells.
How can I do that?

채택된 답변

Guillaume
Guillaume 2019년 12월 18일
편집: Guillaume 2019년 12월 18일
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.
  댓글 수: 1
Uerm
Uerm 2019년 12월 23일
Thanks a lot, both of you!
In my case it would be
threshold = 0.3; %30% threshold
M = cellfun(@(m) mean(m, 2) >= threshold, AL_128, 'UniformOutput', false)
as I do the calculation along the rows.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2019년 12월 13일
Your data didn't seem to have any segments where the value was 1, at least in the several cells I looked in, but I think this should work and be pretty easy to follow and understand.
s = load('AL_128.mat')
AL_128 = s.AL_128
% Define output cell array
output = cell(size(AL_128));
for k = 1 : length(AL_128)
% Get the contents of this cell
thisCellsContents = AL_128{k}; % This is an N rows - by - 20 columns matrix.
[rows, columns] = size(thisCellsContents); % Get the number of rows and columns in this matrix.
% See which rows contain a 1 in any column
containsA1 = any(thisCellsContents == 1, 2); % This is a N row vector.
% See if the number of them is 30% or more of the rows
fractionContaining1 = sum(containsA1) / rows;
if fractionContaining1 >= 0.3
output{k} = ones(rows, 1); % Make vector of all 1's
else
output{k} = thisCellsContents; % Just the original contents.
end
end
% Overwrite input, if desired.
AL_128 = output
  댓글 수: 10
Image Analyst
Image Analyst 2019년 12월 16일
OK, the picture helped. Try this:
s = load('AL_128.mat')
AL_128 = s.AL_128
% Define output cell array
output = cell(size(AL_128));
for k = 1 : length(AL_128)
% Get the contents of this cell
thisCellsContents = AL_128{k}; % This is an N rows - by - 20 columns matrix.
[rows, columns] = size(thisCellsContents); % Get the number of rows and columns in this matrix.
% See which rows contain a 1 in any column
num1sPerRow = sum(thisCellsContents == 1, 2); % This is a N row vector.
% See if the number of them is 30% or more of the columns.
fractionContaining1 = num1sPerRow / columns;
fprintf('The highest fraction of ones any row in matrix #%d has is %.2f.\n', k, max(fractionContaining1));
% Say what rows need to be set to 1 and which need to be set to zero.
indexesToSetTo1 = fractionContaining1 > 0.3;
if any(indexesToSetTo1)
fprintf('Setting cell #%d to a %d-row column vector\n', k, rows);
output{k} = indexesToSetTo1; % Make vector of 0's and 1's
else
fprintf('Leaving cell #%d alone.\n', k);
output{k} = thisCellsContents; % Just the original contents.
end
end
% Overwrite input, if desired.
% AL_128 = output
Uerm
Uerm 2019년 12월 18일
Thanks a lot! I found that this works too:
function M = threshold(AL_128,n)
for i = 1:length(AL_128)
for k = 1:size(AL_128{1,i},1)
M{i}(k,:) = double(mean(AL_128{1,i}(k,:))>=(n/100));
end
end

댓글을 달려면 로그인하십시오.

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by