Num of max consective occurance of 'T'
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
I have a set of cyclone data that I am trying to analyse. in the sample timetable, each system is described by unique id, for each id i want to count the MAX CONSECTIIVE occuance of 'T' in the 'thrsh' column. I also want to know the closest location of the next or previous 'T' after of before the max conseitive occuance for each system. the final table should be [id, maxOcc, nextLoc]. Some selected ids are shown below:
id = 1990S001, maxOcc = 2, nextT = 0 (no occurance of next or previous T after or before F)
id = 1990S005, maxOcc = 2, nextT = 0
id = 1990S014, maxOcc = 5, nextT = -8 (8th occurance of T before F)
id = 1990S019, maxOcc = 3, nextT = -2 (2nd occurance of T before F)
High appreciate your help.
Thanks
댓글 수: 2
Ganesh
2024년 6월 17일
To clarify, 1990S005 has only 2 entries but you have mentioned that it has a maxOcc of 5. Does this mean that the consecutive Ts should be considered in the system 1990S006 too?
채택된 답변
Ganesh
2024년 6월 17일
편집: Ganesh
2024년 6월 17일
You can use the following code:
load('Consective Occ of T.mat')
[uniqueIDs, maxConsecutiveOnes, closestOneIndices] = findMaxConsecutiveOnesAndClosest(barra1.id,cell2mat(barra1.thrsh));
table(uniqueIDs,maxConsecutiveOnes,closestOneIndices)
function [uniqueIDs,maxConsecutiveOnes,closestOneIndices] = findMaxConsecutiveOnesAndClosest(ids, boolChars)
uniqueIDs = unique(ids);
maxConsecutiveOnes = zeros(length(uniqueIDs),1);
closestOneIndices = zeros(length(uniqueIDs),1);
for i = 1:length(uniqueIDs)
id = uniqueIDs(i);
boolValues = boolChars(ids == id) == 'T'; % Filters out only values for each ID
[maxConsecutive, endIndex] = maxConsecutiveOnesHelper(boolValues);
maxConsecutiveOnes(i) = maxConsecutive;
if endIndex == 0 %If there are no consecutive 1s, skip
closestOneIndex = inf;
else
closestOneIndex = findClosestOne(boolValues, endIndex, maxConsecutive);
end
closestOneIndices(i) = closestOneIndex;
end
end
function [maxConsec, endIndex] = maxConsecutiveOnesHelper(boolValues)
maxConsec = 0;
currentConsec = 0;
endIndex = 0;
% Manually checking the longetst consecutive 1s
% Can be optimized
for i = 1:length(boolValues)
if boolValues(i) == true
currentConsec = currentConsec + 1;
if currentConsec > maxConsec
maxConsec = currentConsec;
endIndex = i;
end
else
currentConsec = 0;
end
end
end
function closestIndex = findClosestOne(boolValues, maxEndIndex, maxConsecutive)
if maxConsecutive == length(boolValues)
closestIndex = inf; % I am using inf to denote no close element is present
return;
end
startIndex = maxEndIndex - maxConsecutive + 1;
% Find the closest distance before the consecutive 1s
closestBefore = find(boolValues(1:startIndex-1), 1, 'last');
if ~isempty(closestBefore)
closestIndexBefore = closestBefore-length(boolValues(1:startIndex-1))-1;
else
closestIndexBefore = inf;
end
% Find the closest distance after the consective 1s
closestAfter = find(boolValues(maxEndIndex+1:end), 1, 'first');
if ~isempty(closestAfter)
closestIndexAfter = closestAfter;
else
closestIndexAfter = inf;
end
% Find the minimum
closestIndex = min(closestIndexBefore,closestIndexAfter);
end
I have implemented a straight forward algorithm, feel free to optimize it!
EDIT: Made a small code fix and added comments for readability!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!