How to find the position in cell arry?

Hi
I have cell array data example. D = {[1 2];[1 4];[1 6];[2 4];[2 5];}
I want to find the position of [1 6] in that D, and the answer is 3.
I try this command
suppose c = [1 6]
cellfun(@(x) ismember(x,c),D,'UniformOutput',false)
and the output is below
[0] [0] [1] [0] [0]
but I want the answer is 3
how to do it?
thank you

 Akzeptierte Antwort

Honglei Chen
Honglei Chen am 25 Jun. 2012

0 Stimmen

D = {[1 2];[1 4];[1 6];[2 4];[2 5];}
c = [1 6]
[~,~,idx] = intersect(c,cell2mat(D),'rows')

3 Kommentare

Andrei Bobrov
Andrei Bobrov am 25 Jun. 2012
[k2,k2] = ismember(c,cell2mat(D),'rows');
alice
alice am 25 Jun. 2012
thank you so much, I can apply this command for my work.
However,
[~,~,idx], it's error.
but if i define any variable replace [~,~,idx], example [m,n,idx], it's not error
Honglei Chen
Honglei Chen am 25 Jun. 2012
~ is introduced recently, indicating that the output is not needed in successive steps. If it errors out, you can just use any variable name you want, as you have tried.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

alice
alice am 25 Jun. 2012

0 Stimmen

Dear, Andrei bobrov
your answer is also work.
thank you.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by