get a matrix from a sorted cell array
Show older comments
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
Answers (0)
Categories
Find more on Matrix Indexing 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!