## find array inside a cell

Naime

### Naime (view profile)

さんによって質問されました 2019 年 1 月 15 日

### Stephen Cobeldick (view profile)

さんによって 編集されました 2019 年 1 月 22 日
Stephen Cobeldick

### Stephen Cobeldick (view profile)

さんの 回答が採用されました
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

#### 0 件のコメント

サインイン to comment.

## 2 件の回答

2019 年 1 月 15 日

### Stephen Cobeldick (view profile)

2019 年 1 月 15 日
採用された回答

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 件のコメント

Stephen Cobeldick

### Stephen Cobeldick (view profile)

2019 年 1 月 22 日
@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];
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?
Naime

### Naime (view profile)

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)
Stephen Cobeldick

### Stephen Cobeldick (view profile)

2019 年 1 月 22 日
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

サインイン to comment.

2019 年 1 月 15 日

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

#### 0 件のコメント

サインイン to comment.

Translated by