find array inside a cell
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I have a A={[1,2,4],[2,3,7],[2,5],[4,5,6]} and B=[1;2;3;6;8]
I want to write a code that find B inside A.
the result should be c={[1,2],[2,3],[2],[6]}.
I try this code
c=cell(1, size(A,2));
for i=1:size(A,2)
current=A{i};
for j=1:size(B,1)
index_X = find(A{i} == B(j));
if isempty(index_X)==0
c{i}(end+1)=B(index_X);
end
end
end
but it does not work
채택된 답변
Simpler in one line:
>> A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
>> B = [1;2;3;6;8];
>> C = cellfun(@(m)intersect(m,B),A,'uni',0);
>> celldisp(C)
C{1} =
1
2
C{2} =
2
3
C{3} =
2
C{4} =
6
댓글 수: 14
if B is matrix
how can I find row of B that one of the element (whether column 1 or 2 of B) is include A
A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
B=[1,2;1,5;2,3;2,4;2,5;3,4;4,7]
result should be C_out={[2,3,5,6,7],[1,3,4,5,7],[1,2,3,4],[2,4,5,6,7]}
Stephen23
2019년 1월 15일
@Naime Ahmadi: I do not follow the logic of your example. When I check for the "row of B that one of the element (whether column 1 or 2 of B) is include A" for the first cell of A, I get this:
[
1 % B(1,1)==1==A{1}(1)
2 % B(2,1)==1==A{1}(1)
3 % B(3,1)==2==A{1}(2)
4 % B(4,1)==2==A{1}(2)
5 % B(5,1)==2==A{1}(2)
6 % B(6,2)==4==A{1}(3)
7 % B(7,1)==4==A{1}(3)
]
Please explain how you derived your examples.
A={[1,2,4]}
B=[1,2; this row is not acceptable as both 1 and 2 are include in A
1,5; this row is acceptable as one element (1) include in A
2,3; this row is acceptable as one element (2) include in A
2,4] this row is not acceptable as both 2 and 4 are include in A
answer should be C_out={[1,4]}
if A={[1,2,4],[2,3],[1,2,5]}
answer should be C_out={[2,3],[1,2,4],[3,4]}
>> A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
>> B = [1,2;1,5;2,3;2,4;2,5;3,4;4,7];
>> F = @(v)find(sum(any(v==permute(B,[1,3,2]),2),3)==1);
>> C = cellfun(F,A,'uni',0);
>> celldisp(C)
C{1} =
2
3
5
6
7
C{2} =
1
4
5
6
7
C{3} =
1
2
3
4
C{4} =
2
4
5
6
7
Your C_out{2} example seems to be incorrect.
Thank you.
But I have a problem with permute(B,[1,3,2]).
I understand permute, rearrange the dimensions but I do not understand how?
I try permute(B,[1,2,3]) but I do not know why the ansewer is
1 2
1 5
2 3
2 4
2 5
3 4
4 7
"I try permute(B,[1,2,3]) but I do not know why the ansewer is"
That vector won't do anything. That vector specifies that the dimensions should be arranged 1->1, 2->2, and 3->3, so nothing changes. In contrast, the vector I used [1,3,2] swaps the dimensions like this: 1->1, 2->3, and 2->3.
NA
2019년 1월 17일
I have aquestion about this
A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
B = [1;2;3;6;8];
C = cellfun(@(m)intersect(m,B),A,'uni',0);
insted of the element I want to find a rows. so I try this one but it is not give me following answer
C = cellfun(@(m)find(m==B),A,'uni',0);
C{1} =
1
2
C{2} =
2
3
C{3} =
2
C{4} =
4
Just use the outputs of intersect:
>> [C,X,Y] = cellfun(@(m)intersect(m,B),A,'uni',0);
>> Y{:}
ans =
1
2
ans =
2
3
ans =
2
ans =
4
Thank you. What does X give?
"What does X give?"
Open the intersect documentation, then find the description of the second output argument: that is what it gives. Reading the documentation is how MATLAB users know what functions do.
A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
B = [1,2;1,5;2,3;2,4;2,5;3,4;4,7];
F = @(v)find(sum(any(v==permute(B,[1,3,2]),2),3)==1);
C = cellfun(F,A,'uni',0);
How can I change this code to only look at first column, element should be only in the first column
A={[1,2,4]}
B=[3,4 this row is not acceptable as first element is not in the A
answer should be={[1,2,5,7],[4,5,6],[3,4],[]}
@Naime Ahmadi: you really are going to have to explain the logic of that. Given these inputs:
A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
B = [1,2;1,5;2,3;2,4;2,5;3,4;4,7];
"answer should be ={[1,2,5,7],[4,5,6],[3,4],[]} "
Lets compare the values in the first column of B against the first cell of A:
1 -> matches A{1}(1)
1 -> matches A{1}(1)
2 -> matches A{1}(2)
2 -> matches A{1}(2)
2 -> matches A{1}(2)
3 -> no match
4 -> matches A{1}(3)
So this would give matches for rows [1,2,3,4,5,7]. Where do you get [1,2,5,7] from?
And for the second cell of A:
1 -> no match
1 -> no match
2 -> matches A{2}(1)
2 -> matches A{2}(1)
2 -> matches A{2}(1)
3 -> matches A{2}(2)
4 -> no match
So this would give matches for rows [3,4,5,6]. Where do you get [4,5,6] from?
Please explain carefully. I cannot read your mind, only what you write.
NA
2019년 1월 22일
first column of B against the first cell of A:
A=[1,2,4]
B=[1,2;1,5;2,3;2,4;2,5;3,4;4,7]
1,2 -> Not matches as both (1,2) is in A
1,5 -> matches A{1}(1) (first column is in A but second column is not in A)
2,3 -> matches A{1}(2) (first column is in A but second column is not in A)
2,4-> Not matches A{1}(2) as both (2,4) is in A
2,5-> matches A{1}(2) (first column is in A but second column is not in A)
3,4-> no match (4 is in A but as it is in second column of B it is not match)
4,7 -> matches A{1}(3) (first column is in A but second column is not in A)
Now you seem to indicate that it should be [2,3,5,7] for the first cell. Which is correct?
Perhaps this does what you want:
>> A = {[1,2,4],[2,3,7],[2,5],[4,5,6]};
>> B = [1,2;1,5;2,3;2,4;2,5;3,4;4,7];
>> F = @(v)find(any(v==B(:,1),2)&~any(v==B(:,2),2));
>> C = cellfun(F,A,'uni',0)
>> C{:}
ans =
2
3
5
7
ans =
4
5
6
ans =
3
4
ans =
7
추가 답변 (1개)
madhan ravi
2019년 1월 15일
편집: madhan ravi
2019년 1월 15일
A={[1,2,4],[2,3,7],[2,5],[4,5,6]} ;
B=[1;2;3;6;8];
Result=cell(1,numel(A)); % preallocate
for i = 1:numel(A)
idx=ismember(A{i},B);
Result{i}=A{i}(idx);
end
celldisp(Result)
Gives:
Result{1} =
1 2
Result{2} =
2 3
Result{3} =
2
Result{4} =
6
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
참고 항목
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
