필터 지우기
필터 지우기

How can I use the find function for cell arrays to search for particular cell vector values

조회 수: 3 (최근 30일)
Suppose I have a cell array:
EC = {[1,2,1,1] [1,3,1,1] [1,4,1,1] [1,1,1,2]}
I need a way to index where the values of the cells = [1,3,1,1] or [1,1,1,2] such that MATLAB returns: [2,4] for the 2nd and 4th cells of array EC.
I've been trying to use the find function, but it doesn't appear you can use that for cell arrays...

채택된 답변

KSSV
KSSV 2017년 3월 21일
EC = {[1,2,1,1] [1,3,1,1] [1,4,1,1] [1,1,1,2]} ;
k = {[1,3,1,1] [1,1,1,2]} ;
EC = cell2mat(EC(:)) ;
k = cell2mat(k(:)) ;
[val,idx] = ismember(k,EC,'rows') ;
idx
  댓글 수: 2
Kevin
Kevin 2017년 3월 27일
What does the "val" do? It just seems to be creating a vector of ones corresponding to the length of k (whatever the # of cells I'm looking for), which in this example happens to be 2. How is this ever useful?
Reading help ismember, which calls it LIA (not sure why) says it's just that, an array of same size as 'k', containing true where the elements of 'k' are in 'EC' and false otherwise.
I can understand wanting an index like LOCB (as they call it), and can see how you might just want the number of cases where k is in EC, but how is it useful to just get an array of ones?
Jan
Jan 2017년 3월 27일
편집: Jan 2017년 3월 27일
@Kevin: In the general case this command searchs if an element of A occurres in B:
[LIA, LocB] = ismember(A, B);
LIA is a logical index vector related to A (L I A), which is TRUE, if the corresponding element of A occurres in B, while LocB is the index of the matching element of B.

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

추가 답변 (1개)

John BG
John BG 2017년 3월 25일
hi Kevin
the following builds a 2 columns cell where 1st column is just the numeral coinciding with EC amount of elements and 2nd column is variable length indicating what vector of D matches the i-th element of D
EC = {[1,2,1,1] [1,3,1,1] [1,4,1,1] [1,1,1,2]}
D={[1,3,1,1] [1,1,1,2] }
match={}
for k=1:1:size(EC,2)
match{k,1}=k;
for s=1:1:size(D,2)
if isequal(class(D{s}),class(EC{k})) && isequal(EC{k},D{s})
match{k}=[match{k} s];
end
end
end
match{:}
ans =
1
ans =
2 1
ans =
3
ans =
4 2
this script can be enhanced to include EC and D containing different classes.
if you find these lines useful would you please mark my answer as Accepted Answer?
To any other reader, if you find this answer of any help please click on the thumbs-up vote link,
thanks in advance for time and attention
John BG

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by