determine range within an array
1 次查看(过去 30 天)
显示 更早的评论
I'm trying to determine a range within a range. I have an array of only the start and end time column shown below. The diff and type column was added after running the script. If the difference is 1, the type is check, if it's 3-5, the type is good, if it's 6-11, the type is bad, and if it's 12290 the type is cycle.
start end diff type
2 3 1 check
5 8 3 good
14 24 10 bad
55 12345 12290 cycle (1)
22352 22353 1 check
22389 22397 8 bad
22405 22415 10 bad
22420 22425 5 good
22440 22450 10 bad
22499 34789 12290 cycle (2)
35889 35890 1 check
35901 35906 5 good
35911 35915 4 good
35955 35960 5 good
36002 48292 12290 cycle (3)
The type "check" signifies the start of a cycle and all the "good/bad" ranges after that belongs to the cycle right below it. So far I've been able to determine the type of ranges and how many there are total but I cant figure out how to place them in the correct cycle. Below is the desired output for this array.
cycle good bad
1 1 1
2 1 3
3 3 0
1 个评论
采纳的回答
TAB
2011-10-8
Considering last column of 'type' stored in a cell array, you can find number of 'good' and 'bad' in each cycle with below code
typecell={'check';
'good';
'bad';
'cycle';
'check';
'bad';
'bad';
'good';
'bad';
'cycle';
'check';
'good';
'good';
'good';
'cycle' };
ck_idx=find(strcmp(typecell,'check'));
cy_idx=find(strcmp(typecell,'cycle'));
gd_nos=zeros(length(cy_idx),1);
bd_nos=zeros(length(cy_idx),1);
for r=1:length(cy_idx)
tmpc=typecell(ck_idx(r)+1:cy_idx(r)-1);
gd_nos(r)=length(find(strcmp(tmpc,'good')));
bd_nos(r)=length(find(strcmp(tmpc,'bad')));
end
% If you want to write output result in text file
fh=fopen('myfile.txt','w');
fprintf(fh,'cycle\tgood\tbad\n');
for r=1:length(cy_idx)
fprintf(fh,'%d\t\t%d\t\t%d\n',r,gd_nos(r),bd_nos(r));
end
fclose(fh);
I think this is what you expect.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!