A={[1,2,3,4,5,8,9,39],[2,3,17,18,25,26,27],[3,4,14,15,16,17,18],[4,5,6,11,12,13,14],[5,6,7,8],...
[10,11,12,13],[16,21,22,23,24],[26,28,29],[2,30],[6,31],[10,32],[19,33],[20,34],[22,35],[23,36],[25,37],[29,38]};
ref=31;
base = cellfun(@(m)any(ismember(m,ref)),A,'uni',0);
base_mes=A{find([base{:}]==1)};
include_base = cellfun(@(m)any(ismember(m,base_mes)),A,'uni',0);
result=cell(1,numel(include_base));
index_other=find([include_base{:}]==1);
for i=1:size(index_other,2)
result{i}=A{index_other(i)};
end
base_mes=[6,31], I want to find 6 and 31 in A, after this sort A according to 31 and 6.
result={[6,31],[4,5,6,11,12,13,14],[5,6,7,8],[1,2,3,4,5,8,9,39],[2,3,17,18,25,26,27],[3,4,14,15,16,17,18],[10,11,12,13],[16,21,22,23,24],[26,28,29],[2,30],[10,32],[19,33],[20,34],[22,35],[23,36],[25,37],[29,38]}

댓글 수: 5

Jos (10584)
Jos (10584) 2019년 2월 20일
What does "sort A according to 31 and 6" mean, exactly?
NA
NA 2019년 2월 20일
편집: NA 2019년 2월 20일
ref=31, I need to find an array that includes 31. so [6,31] is found, after this, as [6,31] is included 6, so need to find arrays that is included 6. in this case it becomes [4,5,6,11,12,13,14], [5,6,7,8].
finally the order of array in cell A changes.[6,31] become first, [4,5,6,11,12,13,14] second and [5,6,7,8] third and put other left array in A
Jos (10584)
Jos (10584) 2019년 2월 20일
Apparently you question has been answered, but what if
  • there are more cells that contain 31
  • what if the cell(s) containing 31, contain more than 2 numbers
I am just curious ...
NA
NA 2019년 2월 20일
편집: NA 2019년 2월 20일
good point. How should I fix it?
Stephen23
Stephen23 2019년 2월 20일
편집: Stephen23 2019년 2월 20일
  • "there are more cells that contain 31" -> "How should I fix it?" -> only you can decide how to "fix" that, or if it needs "fixing" at all. You can tell us what you want to happen, but we cannot tell you what you want to happen in that situation.
  • "what if the cell(s) containing 31, contain more than 2 numbers" -> my answer does not assume anything about how many elements the vectors have.

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

 채택된 답변

Stephen23
Stephen23 2019년 2월 20일
편집: Stephen23 2019년 2월 20일

0 개 추천

You can easily use logical indexing for this:
A = {[1,2,3,4,5,8,9,39],[2,3,17,18,25,26,27],[3,4,14,15,16,17,18],[4,5,6,11,12,13,14],[5,6,7,8],[10,11,12,13],[16,21,22,23,24],[26,28,29],[2,30],[6,31],[10,32],[19,33],[20,34],[22,35],[23,36],[25,37],[29,38]};
ref = 31
idr = cellfun(@(v)any(ismember(v,ref)),A);
vec = A{idr};
idv = cellfun(@(v)any(ismember(v,vec)),A);
Z = [A(idr),A(idv&~idr),A(~idv)];
Giving:
>> Z{:}
ans =
6 31
ans =
4 5 6 11 12 13 14
ans =
5 6 7 8
ans =
1 2 3 4 5 8 9 39
ans =
2 3 17 18 25 26 27
ans =
3 4 14 15 16 17 18
ans =
10 11 12 13
ans =
16 21 22 23 24
ans =
26 28 29
ans =
2 30
ans =
10 32
ans =
19 33
ans =
20 34
ans =
22 35
ans =
23 36
ans =
25 37
ans =
29 38

댓글 수: 4

Stephen23
Stephen23 2019년 2월 20일
PS: following on from Jos' comment, if more than one ref match is possible, then you can take them all into account using this line:
vec = [A{idr}];
NA
NA 2019년 2월 21일
편집: NA 2019년 2월 21일
Thank you.
How can I change order inside each cell
Z{:}
ans =
6 31
ans =
4 5 6 11 12 13 14
ans =
5 6 7 8
I want to change above to this
Z{:}
ans =
31 6
ans =
6 4 5 11 12 13 14
ans =
6 5 7 8
as 31 is ref, I want to be first element in each array. Also 6 should be first element.
Stephen23
Stephen23 2019년 2월 21일
>> fun = @(v) sort(0-ismember(v,vec)-(v==ref));
>> [~,ids] = cellfun(fun,Z,'uni',0);
>> Z1 = cellfun(@(v,x)v(x),Z,ids,'uni',0);
>> Z1{:}
ans =
31 6
ans =
6 4 5 11 12 13 14
ans =
6 5 7 8
ans =
1 2 3 4 5 8 9 39
ans =
2 3 17 18 25 26 27
ans =
3 4 14 15 16 17 18
ans =
10 11 12 13
ans =
16 21 22 23 24
ans =
26 28 29
ans =
2 30
ans =
10 32
ans =
19 33
ans =
20 34
ans =
22 35
ans =
23 36
ans =
25 37
ans =
29 38
Stephen23
Stephen23 2019년 2월 26일
@Naime Ahmadi: you can probably do that using a loop or two. Try it!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

태그

질문:

NA
2019년 2월 20일

댓글:

2019년 2월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by