find a row in a multidimensional cell array

Hi, I have a cell variable as follows, temp =
'FBgn0039563' 'FBgn0259937' [0.6195]
'FBgn0039563' 'FBgn0024733' [0.5698]
'FBgn0039563' 'FBgn0011236' [0.5247]
'FBgn0039563' 'FBgn0053864' [0.5155]
'FBgn0039563' 'FBgn0035951' [0.5781]
'FBgn0039563' 'FBgn0001224' [0.5462]
'FBgn0039563' 'FBgn0002914' [0.5162]
'FBgn0039563' 'FBgn0264492' [0.8405]
'FBgn0039563' 'FBgn0000259' [0.7570]
'FBgn0039563' 'FBgn0004103' [0.5374]
I want to search a key =['FBgn0039563' 'FBgn0264492' [0.8405]] inside temp. I need the row index of key in temp. Is it possible?
Thanks in advance,
Best Regards, Wasim

1 commentaire

Wasim Aftab
Wasim Aftab le 26 Sep 2017
Modifié(e) : Wasim Aftab le 26 Sep 2017
Hi, for the attached case, snippets are not yielding correct results, I have a smaller list with N rows and 2 columns, and I want to search them inside a bigger list temp with M rows and two columns. Also if I want to search the otherway around. In both cases it should consider each row as a whole entity and when found accumulate its index in a vector. Is it possible without loops? I will appreciate if the snippet is as general as possible i.e. it can cover most of the test cases. Thanks in advance for your time, Best Regards, Wasim

Connectez-vous pour commenter.

 Réponse acceptée

the cyclist
the cyclist le 4 Juil 2017
Modifié(e) : the cyclist le 4 Juil 2017
Here is one way:
% Your variables
temp = { ...
'FBgn0039563' 'FBgn0259937' [0.6195]
'FBgn0039563' 'FBgn0024733' [0.5698]
'FBgn0039563' 'FBgn0011236' [0.5247]
'FBgn0039563' 'FBgn0053864' [0.5155]
'FBgn0039563' 'FBgn0035951' [0.5781]
'FBgn0039563' 'FBgn0001224' [0.5462]
'FBgn0039563' 'FBgn0002914' [0.5162]
'FBgn0039563' 'FBgn0264492' [0.8405]
'FBgn0039563' 'FBgn0000259' [0.7570]
'FBgn0039563' 'FBgn0004103' [0.5374]};
key = {'FBgn0039563' 'FBgn0264492' [0.8405]};
% Define some temporary string variables
temp2 = cellfun(@num2str,temp,'UniformOutput',false);
key2 = cellfun(@num2str,key, 'UniformOutput',false);
% Compare rows to get the index you want
rowIndex = find(all(ismember(temp2,key2),2));
This solution is based on information I found in this answer, after a keyword search.

2 commentaires

Wasim Aftab
Wasim Aftab le 5 Juil 2017
Hi "the cyclist", It is an awesome snippet. it can find indices of a single row as well as multiple rows. Thanks and Best Regards. Wasim
Wasim Aftab
Wasim Aftab le 26 Sep 2017
Modifié(e) : Wasim Aftab le 26 Sep 2017
Hi, for the attached case, snippets are not yielding correct results, I have a smaller list with N rows and 2 columns, and I want to search them inside a bigger list temp with M rows and two columns. Also if I want to search the otherway around. In both cases it should consider each row as a whole entity and when found accumulate its index in a vector. Is it possible without loops? I will appreciate if the snippet is as general as possible i.e. it can cover most of the test cases. Thanks in advance for your time, Best Regards, Wasim

Connectez-vous pour commenter.

Plus de réponses (2)

Jan
Jan le 4 Juil 2017
Search = {'FBgn0039563' 'FBgn0264492' [0.8405]}
Match = strcmp(temp(:, 1), Search{1}) & ...
strcmp(temp(:, 2), Search{2}) & ...
cat(1, temp{:, 3}) == Search{3};
Index = find(Match);

3 commentaires

Wasim Aftab
Wasim Aftab le 5 Juil 2017
Hi Simon, Thanks for your swift response. Its working for finding a row perfectly but if key = temp(2:4,:) then it cannot find the row indices. Best Regards, Wasim
@Wasim Aftab: And therefore it would be useful, if you explain the needs exactly. In your original question the key was a single row only.
Then:
Match = ismember(temp(:, 1), Search(1, :)) & ...
ismember(temp(:, 2), Search(2, :)) & ...
ismember(cat(1, temp{:, 3}), cat(1, Search{3, :}));
Wasim Aftab
Wasim Aftab le 7 Juil 2017
Hi Simon, Thank you for your time. You are right, its my mistake not to explain it precisely in the question. But when I write code I try to write as general as possible. That's why I assumed answers to be general. However, your code is still not working. Best Regards, Wasim

Connectez-vous pour commenter.

Andrei Bobrov
Andrei Bobrov le 4 Juil 2017
find(all(ismember(temp(:,1:2),key(1:2)),2) & ismember([temp{:,3}]',key{3}))

7 commentaires

Wasim Aftab
Wasim Aftab le 5 Juil 2017
Hi Andrel, Thanks for your quick and succinct answer. Its working for finding a row perfectly but if key = temp(2:4,:) then it cannot find the row indices. Best Regards, Wasim
Andrei Bobrov
Andrei Bobrov le 5 Juil 2017
Modifié(e) : Andrei Bobrov le 5 Juil 2017
:)
find(all(ismember(temp(:,1:2),key(:,1:2)),2) & ismember([temp{:,3}]',[key{:,3}]))
Wasim Aftab
Wasim Aftab le 7 Juil 2017
Hi Andreal, Thanks for your time. Your snippet is working and very compact. Best Regards, Wasim
Andrei Bobrov
Andrei Bobrov le 7 Juil 2017
Thank you Wasim!
Wasim Aftab
Wasim Aftab le 26 Sep 2017
Hi, for the attached case, snippets are not yielding correct results, I have a smaller list with N rows and 2 columns, and I want to search them inside a bigger list temp with M rows and two columns. Also if I want to search the otherway around. In both cases it should consider each row as a whole entity and when found accumulate its index in a vector. Is it possible without loops? I will appreciate if the snippet is as general as possible i.e. it can cover most of the test cases. Thanks in advance for your time, Best Regards, Wasim
[l,ii] = ismember(temp,key,'rows');
Wasim Aftab
Wasim Aftab le 2 Fév 2018
Modifié(e) : Wasim Aftab le 2 Fév 2018
it's not correct implementation. for each row of key found in temp, its index in temp should be recorded, so the output will be an one-dimensional vector. but in your case ii is 2d.

Connectez-vous pour commenter.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by