MATLAB Answers

find values in vector present in cell array

4 ビュー (過去 30 日間)
Elysi Cochin
Elysi Cochin 2020 年 3 月 17 日
編集済み: Andrei Bobrov 2020 年 3 月 17 日
i have a cell array cA and vector V with values
cA = {[11,21,3;14,5,63], [11,22], [33;95;66;7]};
V = [14 22 33 66];
i wanted to find which all values in V are there in cA and its position
output = { [ 0 0 0 1 0 0], [1 2], [3 0 4 0] }

  0 件のコメント

Sign in to comment.

採用された回答

Andrei Bobrov
Andrei Bobrov 2020 年 3 月 17 日
編集済み: Andrei Bobrov 2020 年 3 月 17 日
out = cell(size(cA));
for i = 1:numel(out)
x = cA{i}';
[~,out{i}] = ismember(x(:)',V);
end
or with cellfun
[~,out] = cellfun(@(x)ismember(reshape(x',1,[]),V),cA,'un',0);

  0 件のコメント

Sign in to comment.

その他の回答 (1 件)

Ahmed Anas
Ahmed Anas 2020 年 3 月 17 日
Dear, Always use matrix instead of cell arrays, working with matrix is really easy as compared to cell arrays. In the code below, i have split cA matrix into three small matrices and then checked the entities which are present in vector V. Afterwards compile the entire output matrix.
%%Splitting cA matrix into three consistent matrices
cA1 = [11,21,3;14,5,63]
cA2 = [11,22]
cA3 = [33;95;66;7]
V = [14 22 33 66];
%%Checking for entities of cA1
[Lia,Locb1] = ismember(cA1,V)
reshape(Locb1,1,[])
transpose(Locb1)
Locb1.'
Locb1(:)
Locb1=reshape(Locb1.',1,[])
%%Checking for entities of cA2
[Lia,Locb2] = ismember(cA2,V)
Locb2=reshape(Locb2,1,[])
%%Checking for entities of cA3
[Lia,Locb3] = ismember(cA3,V)
Locb3=reshape(Locb3,1,[])
Output=cell2mat({Locb1,Locb2,Locb3})

  2 件のコメント

Elysi Cochin
Elysi Cochin 2020 年 3 月 17 日
the dimension of cell array changes. so how to keep the code unaltered for any dimension
sometimes
cA = {[11,22,3;44], [14,22]};
V = [14 22 33 66];
output = { [ 0 2 0 0 ], [1 2] }
Ahmed Anas
Ahmed Anas 2020 年 3 月 17 日
I am unable to understand [11 22 3;44] in cA = {[11,22,3;44], [14,22]};
Its dimensions should be consistent

Sign in to comment.


Translated by