Clear Filters
Clear Filters

get a matrix from a sorted cell array

2 views (last 30 days)
stam dadi
stam dadi on 8 Nov 2017
Commented: stam dadi on 8 Nov 2017
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 Comments
KL
KL on 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.
stam dadi
stam dadi on 8 Nov 2017
Yes..I know , my goal is to stop at the first inclusion and go to the next cell ...so A{1,1} is a member of 6 I stop no need to continue , I move into A{2} and go on ...

Sign in to comment.

Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!