keep the index of removed array in cell

조회 수: 1 (최근 30일)
NA
NA 2020년 5월 18일
댓글: Adam Danz 2020년 5월 19일
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
Adam Danz 2020년 5월 18일
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
  댓글 수: 2
NA
NA 2020년 5월 19일
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
Adam Danz 2020년 5월 19일
You may want to use,
M(X,:)= NaN;
instead just in case S ever contains a 0.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modeling에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by