finding specific string in cell arrays

I have a set of alphabet, like: alph={a,b,...,z} And Also a cell array named "repo" in which different combination of alphabets exist and each combination has a value, like:
'x y' 2
'r t' 4
'a b' 1
this array is very big. I want to find different combination of my alphabet in this array and extract the value of it. Note that the array does not contain all the combinations of my alph-set. For example it may does not have 'x b' at all (but we don't know if it exist or not).
I wrote the following code. But I want to avoid for-loop, because it is too slow. Do you know any efficient and fast way?
for i=1:size(alphabet)
for j=i:size(alphabet)
str_tmp = strcat(alphabet(i,1), {' '},alphabet(j,1));
index = find(strcmp(str_tmp,repo(:,1)));
saveSomeWhere = str2double(cell2mat(repo(index,2)));
end
end
Many thanks,

8 Commenti

for the posted example, what is the expected result?
Well, I want to extract the value of words (words= concatenation of alphabets with space) from a repository. In "repo" there are some values for some words with length 2, and I should extract the value of all words from alphabet set: 'a b', 'a c' ... . Some words are not in repo, so the value is zero, others are in repo and I will find the values.
In the written code I used 2 for-loop to loop over the alphabet set to make the word, and then using "find", find the value of word from the "repo". But for-loops are too slow.
Where is the example?
Do you mean the repository? it is a big dataset. It is like the following m*2 matrix:
repo:
'alph1 alph2' 3
'alph3 alph1' 4
'alph2 alph4' 1
....
An the aplahebt set is : {alph1, alph2, alph3, alph4, ...}
And my problem is for-loops which are slow, cause the matrix and alphabet set are large.
I didn't ask you to post all your data, just a short example, then post the expected result. How are we supposed to answer if you are not able to show us the expected result?
Shima Asaadi
Shima Asaadi il 29 Mar 2016
Modificato: Shima Asaadi il 29 Mar 2016
I attached the example. Now, consider the words with length 1 as our alphabet set, and other phrases which have length 2 or more as the "repo". (here, I am just working with phrases of length 2)
In repo, for example there is a phrase : 'deserve appreciation'. also 'deserve' and 'appreciation' are words in my alphabet set (if you search in repo). I want to find the value of this phrase from "repo" that is 2, and do this for all combination of 2-alphabets....
Let us take just this cell array
M={'"' '0'
'"business-as-usual"' '0'
'"celebration,"' '0'
'"cuddly-feely"' '2'
'"detainees"' '2'
'"exemplary"' '4'
'"family"' '4'
'"force"' '0'
'"frighten"' '0'
'"hyperbole,"' '0'}
What are you expecting from this cell array? can you post the expecting result, and explain how to obtain it?
Ok, consider "force" and "family" are 2 of our alphabets. I want to find the value of "force family" ( The values are written in the second column like natural numbers) and this phrase "force family" may have some values in repo or does not have any (we dont knpw). So, I have to search the whole repo.
I wrote this code :
for i=1:size(alphabet_set)
for j=i:size(alphabet_set)
str_tmp = strcat(alphabet_set(i), {' '},alphabet_set(j));
index = find(strcmp(str_tmp,repo(:,1)));
value = str2double(cell2mat(repo(index,2)));
end
end
for this purpose.

Accedi per commentare.

Risposte (0)

Categorie

Richiesto:

il 29 Mar 2016

Modificato:

il 29 Mar 2016

Community Treasure Hunt

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

Start Hunting!

Translated by