# keep the index of removed array in cell

1 view (last 30 days)
NA on 18 May 2020
Commented: Adam Danz on 19 May 2020
I have
M = [12,13; 6,12; 6,13; 7,8; 4,5; 5,6; 9,14; 1,2; 1,5; 13,14; 2,5; 4 9];
S = {[7,8],[6,12,13],[4,5,6,9,13,14],[1,2,5]};
I removed some of the rows in M to get M_New
[~,X] = setdiff(sort(M,2),sort([12 13; 9 14; 2,5],2),'rows','stable');
M_New = M(X,:);
Use below function to find indices of M that matches to S
U = @(v)(find(sum(any(v==permute(M_New,[1,3,2]),2),3)==2))';
ele_in = cellfun(U,S,'uni',0);
For finding the orginal index I use this code
Temp = cellfun(@(x)find(all(ismember(M,M_New(x,:)),2)),ele_in,'uni',0);
But it gives me this result
temp={[4],[1;2;3],[3;5;6;7;10;12],[8;9;11]}
I want to have this result
temp={[4],[2;3],[3;5;6;10;12],[8,9]}

Adam Danz on 18 May 2020
What's the rule for matching values of S to M? For example, row 11 of M contains 2 and 5 which are both listed in S{4} but the output in temp{4} only shows matches for rows 8 and 9 and not row 11.
If I've understood the goal correctly (which may not be the case) and the expected output is a mistake, you can do all of this in 1 line of code,
temp = cellfun(@(c){find(all(ismember(M,c),2))},S);
Result:
temp{1} =
4
temp{2} =
1
2
3
temp{3} =
3
5
6
7
10
temp{4} =
8
9
NA on 19 May 2020
Thank you. I changed the M matrix
[~,X] = intersect(sort(M,2),sort([12 13; 9 14; 2,5],2),'rows','stable');
M(X,:)= 0;
temp = cellfun(@(c){find(all(ismember(M,c),2))},S);
Adam Danz on 19 May 2020
You may want to use,
M(X,:)= NaN;
instead just in case S ever contains a 0.