Subdividing a cell array of ratings data
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Andrei Bobrov
on 16 Apr 2012
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 Comments
See Also
Categories
Find more on Structures 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!