Effacer les filtres
Effacer les filtres

How to group string datasets?

1 vue (au cours des 30 derniers jours)
Matthew
Matthew le 17 Avr 2014
Commenté : Matthew le 22 Avr 2014
I have the following dataset:
ID_Ref SP MD FI
123456 [] [] 'A+'
234567 [] [] 'A'
234567 [] [] 'A'
345678 [] [] 'A+'
345678 [] 'Aa2' []
456789 [] [] 'A+'
456789 [] 'Aa2' []
456789 AA+ 'Aa2' []
All the column arrays are string.
How do I group the above dataset by unique ID_Ref to read:
ID_Ref SP MD FI
123456 [] [] 'A+'
234567 [] [] 'A'
345678 [] 'Aa2' 'A+'
456789 AA+ 'Aa2' 'A+'
Apologies if you are unable to read this table in this message box.

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 17 Avr 2014
Modifié(e) : Azzi Abdelmalek le 17 Avr 2014
Edit
ratings={123456 [] [] 'A+'
234567 [] [] 'A'
234567 [] [] 'A'
345678 [] 'Aa2' 'A+'
456789 'AA+' 'Aa2' 'A+'
345678 [] 'Aa2' []
456789 [] [] 'A+'
456789 [] 'Aa2' []
456789 'AA+' 'Aa2' []}
A=cellfun(@num2str,ratings,'un',0);
c1=cell2mat(ratings(:,1));
[ii,jj,kk]=unique(c1,'stable');
cc=size(A,2);
out=cell(numel(ii),cc);
for k=1:numel(ii)
idx=find(kk==k);
out{k,1}=ii(k,1);
for mm=2:cc
a=A(idx,mm);
b=unique(a);
out{k,mm}=b{end};
end
end
out(cellfun(@isempty ,out))={[]}
  4 commentaires
Matthew
Matthew le 22 Avr 2014
Hi Azzi,
I have used your code above again and it works to a certain extent:
Here is the code I have used but I am getting the following error now:
"Index exceeds matrix dimensions."
This error is occurring from the for line stage.
Here is the code I have used:
Ratings = dataset2cell(Ratings);
Ratings = cellfun(@num2str,Ratings,'un',0);
[ii,jj,kk] = unique(Ratings,'stable');
cc = size(Ratings,2);
Ratings1 = cell(numel(ii),cc);
for k=1:numel(ii)
idx=find(kk==k);
Ratings1{k,1}=ii(k,1);
for mm=2:cc
a=Ratings(idx,mm);
b=unique(a);
Ratings1{k,mm}=b{end};
end
end
Ratings1(cellfun(@isempty ,out)) = {[]};
Can you help me from the for line stage please as it still producing the final unique table with merged rating and how I bypass/ correct so this error message does not appear?
Thanks
Matthew
Matthew le 22 Avr 2014
Okay I have done a bit more digging and it is to with the following function:
[ii,jj,kk] = unique(Ratings,'stable');
It is taking into account the Rating column headers i.e. SP, MD and FI and the values these columns can take and then putting that in the ii table at the bottom.
Any suggestions how to make the function work properly?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by