Subdividing a cell array of ratings data

1 回表示 (過去 30 日間)
AJP
AJP 2012 年 4 月 16 日
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?

回答 (1 件)

Andrei Bobrov
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'))

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by