필터 지우기
필터 지우기

Finding valid sequences in a list based on specific conditions in matlab

조회 수: 3 (최근 30일)
I have a list of scores represented as a cell array in MATLAB, and I need to extract valid sequences from this list based on certain conditions. The scores can be one of three stages: 'Rain', 'Cloud', or 'Sun'.
The conditions for a valid sequence are as follows:
For a sequence of 'Sun' stages, the sequence must have a length of at least 7.
For a sequence of 'Rain' or 'Cloud' stages, the sequence must have a length of at least 7, and all stages within the sequence must be either 'Rain' or 'Cloud'.
I have tried implementing the following code, but it does not provide the correct results for all cases:
scores = {'Rain' 'Cloud' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Rain'};
sequence_start = 1;
num_scores = numel(scores);
valid_sequences = [];
for j = 1:num_scores
stage = scores{j};
if strcmp(stage, 'Sun')
sequence_length = j - sequence_start + 1;
if sequence_length >= 7
valid_sequences = [valid_sequences; sequence_start, j];
end
elseif ismember(stage, {'Cloud', 'Rain', 'Thunder'})
sequence_length = j;
if sequence_length >= 7 && all(ismember(scores(sequence_start:j), {'Cloud', 'Rain', 'Thunder'}))
valid_sequences = [valid_sequences; sequence_start, j];
end
sequence_start = j + 1; % Reset sequence_start
else
sequence_start = j; % Reset sequence_start
end
end
if (num_scores - sequence_start + 1) >= 7
valid_sequences = [valid_sequences; sequence_start, num_scores];
end
this gives me the results valid_sequences = 3, 9 which is correct (Sun starts at position 3 and ends at position 9).
However, when I put the following test cases in:
test 1:
scores = {'Rain' 'Rain' 'Cloud' 'Cloud' 'Rain' 'Cloud' 'Sun' 'Cloud' 'Rain'};
I get the answer valid_sequences = 9,9
test 2:
scores = {'Sun' 'Rain' 'Cloud' 'Cloud' 'Rain' 'Cloud' 'Rain' 'Cloud' 'Thunder' 'Sun'};
valid_sequences =
7 7
8 8
9 9
Which suggests there seems to be something wrong with how the Rain/Cloud/Thunder gets read in after Sun.
I would be very grateful if you could help!

채택된 답변

Bruno Luong
Bruno Luong 2023년 7월 19일
편집: Bruno Luong 2023년 7월 19일
See if the results are what you expect (if not please explain why)
getvalidseq({'Rain' 'Cloud' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Rain'})
ans = 1×2
3 9
getvalidseq({'Rain' 'Rain' 'Cloud' 'Cloud' 'Rain' 'Cloud' 'Sun' 'Cloud' 'Rain'})
ans = 0×2 empty double matrix
getvalidseq({'Sun' 'Rain' 'Cloud' 'Cloud' 'Rain' 'Cloud' 'Rain' 'Cloud' 'Thunder' 'Sun'})
ans = 1×2
2 9
function valid_sequences = getvalidseq(scores)
b = [true; diff(ismember(scores(:), {'Rain' 'Cloud' 'Thunder'})); true];
i = find(b);
n = diff(i);
j = find(n>=7);
valid_sequences = [i(j) i(j+1)-1];
end
  댓글 수: 5
Bruno Luong
Bruno Luong 2023년 7월 19일
편집: Bruno Luong 2023년 7월 19일
OK this should do it.
getvalidseq({'Rain' 'Cloud' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Sun' 'Rain'})
ans = 1×2
3 9
getvalidseq({'Rain' 'Rain' 'Cloud' 'Cloud' 'Rain' 'Cloud' 'Sun' 'Cloud' 'Rain'})
ans = 2×2
1 6 8 9
getvalidseq({'Sun' 'Rain' 'Cloud' 'Cloud' 'Rain' 'Cloud' 'Rain' 'Cloud' 'Thunder' 'Sun'})
ans = 1×2
2 9
function valid_sequences = getvalidseq(scores)
b = [true; diff(ismember(scores(:), {'Rain' 'Cloud' 'Thunder'})); true];
i = find(b);
n = diff(i);
nn = size(n,1);
j1 = (1:2:nn)';
j2 = (2:2:nn)';
jj = {j1, j2};
jj = jj(cellfun(@(j) sum(n(j)), jj) >= 7);
j = cat(1,jj{:});
valid_sequences = [i(j) i(j+1)-1];
end
PS: I reread your original question and you never state the merge thing. I'm notorious here to dislike when people asking incompete question, or change the request during the thread is developping.
nines
nines 2023년 7월 20일
Thank you so so much!!!! I am so sorry for the lack of clarity, I will do better going forward. Thanks so incredibly much, this moves my work forward a ton!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel and Cloud에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by