Subdividing a cell array of ratings data
조회 수: 1 (최근 30일)
이전 댓글 표시
I have a Nx3 cell array that describes how the rating of an asset ID changes with time. It looks something like this:
{'ID1','01-Jan-2001','A';
'ID1','05-Mar-2003','D';
'ID1','15-Dec-2007','B';
'ID1','15-Dec-2007','B';
'ID1','23-May-2009','A';
'ID2','05-Mar-2003','B';
'ID2','15-Dec-2007','D';
'ID2','29-Apr-2011','A';
'ID3','05-Jun-2003','C';
'ID4','15-Feb-2002','C';
..........etc..........}
The first column is an ID string, the second column is a date string and the third column is a rating string. The columns are sorted first by ID then date. There is one or more rating for each ID.
I want make a split (by modifying the ID) whenever the rating time series shows improvement. The result for the example above would look like this:
{'ID1','01-Jan-2001','A';
'ID1','05-Mar-2003','D';
'ID1_1','15-Dec-2007','B';
'ID1_1','15-Dec-2007','B';
'ID1_2','23-May-2009','A';
'ID2','05-Mar-2003','B';
'ID2','15-Dec-2007','D';
'ID2_1','29-Apr-2011','A';
'ID3','05-Jun-2003','C';
'ID4','15-Feb-2002','C';
..........etc..........}
The ID is modified by appending a number whenever the rating, for a given ID, in a given row, is better than the rating in the previous row.
I have given some thought to a loop-based method of doing this but perhaps there is a much faster way based on logical indexing?
댓글 수: 0
답변 (1개)
Andrei Bobrov
2012년 4월 16일
ID= {'ID1','01-Jan-2001','A';
'ID1','05-Mar-2003','D';
'ID1','15-Dec-2007','B';
'ID1','15-Dec-2007','B';
'ID1','23-May-2009','A';
'ID2','05-Mar-2003','B';
'ID2','15-Dec-2007','D';
'ID2','29-Apr-2011','A';
'ID3','05-Jun-2003','C';
'ID4','15-Feb-2002','C';}
[n n n] = unique(ID(:,3));
[d d d] = unique(ID(:,1));
k = [1;diff(n)] < 0;
t = histc(d,1:max(d));
idx = cell2mat(cellfun(@cumsum,mat2cell(k,t,1),'un',0));
tt = idx ~= 0;
ID(tt,1) = strcat(ID(tt,1),'_',num2str(idx(tt),'%-d'))
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!