get a matrix from a sorted cell array
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have the following cell array of size 1x9
A= {{'O1'} ,{'O1','O2','O3','O4'} ,{'O1','O3'} ,{'O1','O2','O3','O4'} , {},{'O1','O2','O3','O4'},{'O1','O3'},{'O1','O2','O3','O4'},{'O1','O2','O3','O4'}};
I want to get a matrix adj of size 9x9 as follows
first, I want to sort the cell array according to the length of cells starting from A{5}, using the `sort` function
the result will be: {'O1','O3'} will be moved from A{7} to A{6}
A= {{'O1'} ,{'O1','O2','O3','O4'} ,{'O1','O3'} ,{'O1','O2','O3','O4'} ,{},{'O1','O3'},{'O1','O2','O3','O4'},{'O1','O2','O3','O4'},{'O1','O2','O3','O4'}};
now, to fill the matrix , I want to compare one by one the first 4 cells of A (i=1:4) with the remaining cells (j=5:9) where if A{i} is included in A{j} then adj(i,j)=1 . the difficulty is I want this last j to be the index before the sorting and I want to exit the compraison loop as soon as one inclusion is found. For exemple : A{1}={'O1'} is included in A{6}={'O1','O3'} so since the index of {'O1','O3'} was 7 before the sorting then adj(1,7)=1 .
I must have :
adj (1,7)=1 ; adj (2,6)=1 ; adj (3,7)=1 ; adj (4,6)=1 ;
I tried this so far :
A= sort(cellfun('length',A{5:9}));
for i=1:4
for j=5:9
if ismember(A{i},A{j})
adj(i,j)=1; % how to get the first index ??
break
end
end
end
2 commentaires
KL
le 8 Nov 2017
A{1,1} is a member of every cell from 6 to 9 and A{1,2} is exactly the same as the cells from 7 to 9. Your example is not very clear.
Réponses (0)
Voir également
Catégories
En savoir plus sur Shifting and Sorting Matrices 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!