Finding valid sequences in a list based on specific conditions in matlab
Show older comments
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!
Accepted Answer
More Answers (0)
Categories
Find more on Licensing on Cloud Platforms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!