I have a matrix that contains strings and i have to find words in it.

13 vues (au cours des 30 derniers jours)
Ziyad Azouny
Ziyad Azouny le 19 Fév 2020
Commenté : Ziyad Azouny le 6 Mar 2020
I have to create a program that gives you the position of the first letter of the word you input and if it's from left to right or right to left.
for example if you input the word 'er' it should tell you that:
The word was found from left to right at the position (1,1)
Please help
['e' 'r' 'd' 'u' 'o' 'p' 'i' 'e' 'c' 'e';...
't' 'p' 'r' 'o' 'm' 'e' 't' 'a' 'l' 'l';...
'i' 's' 'd' 'p' 's' 't' 'u' 'b' 'a' 'a';...
'g' 'e' 'p' 'o' 'm' 's' 'i' 'c' 'm' 'v';...
's' 't' 'e' 'i' 'u' 'r' 'a' 't' 'e' 'a';...
'o' 't' 'n' 'n' 'c' 'b' 'c' 'b' 'r' 'g';...
'u' 'e' 'f' 'c' 't' 'e' 'l' 'u' 'l' 'e';...
'v' 'l' 'i' 'o' 'r' 'b' 'r' 'o' 'i' 'e';...
'e' 'l' 'e' 'n' 'u' 'u' 'i' 'r' 'n' 't';...
'r' 'i' 'v' 'i' 'e' 'r' 'e' 'j' 'g' 'a';...
'a' 'a' 'r' 'n' 'e' 'l' 'r' 'r' 'o' 'c';...
'i' 'p' 'e' 'c' 'a' 'r' 'a' 't' 't' 'u';...
'n' 't' 'e' 'g' 'a' 'i' 'l' 'l' 'a' 'd';...
'f' 'i' 'l' 'o' 'n' 'o' 'l' 'a' 't' 'e']
  2 commentaires
Mil Shastri
Mil Shastri le 19 Fév 2020
Hi Ziyad, could you rephrase your question. It's not very clear.
Stephen23
Stephen23 le 3 Mar 2020
Modifié(e) : Stephen23 le 3 Mar 2020
The character array shown in the question can be written more simply as:
['erduopiece';...
'tprometall';...
'isdpstubaa';...
'gepomsicmv';...
'steiuratea';...
'otnncbcbrg';...
'uefctelule';...
'vliorbroie';...
'elenuuirnt';...
'rivierejga';...
'aarnelrroc';...
'ipecarattu';...
'ntegaillad';...
'filonolate']
or:
['erduopiece';'tprometall';'isdpstubaa';'gepomsicmv';'steiuratea';'otnncbcbrg';'uefctelule';'vliorbroie';'elenuuirnt';'rivierejga';'aarnelrroc';'ipecarattu';'ntegaillad';'filonolate']

Connectez-vous pour commenter.

Réponse acceptée

Jalaj Gambhir
Jalaj Gambhir le 3 Mar 2020
Hi,
You can use strfind function to determine the presence of elements in you char array.
matrix = ['a','b','d','f';
'c','t','a','g';
'f','a','n','p';
'd','b','a','b'];
word = 'ab';
for i=1:length(matrix)
str = matrix(i,:);
idxL2R = strfind(str,word);
idxR2L = strfind(str,flip(word));
result = ['for string = ',str,' indexL2R while searching for word = ',word,
' is = ',int2str(idxL2R), ' indexR2L is = ', int2str(idxR2L)];
disp(result);
end
  2 commentaires
Jalaj Gambhir
Jalaj Gambhir le 3 Mar 2020
Result:
for string = abdf indexL2R while searching for word = ab is = 1 indexR2L is =
for string = ctag indexL2R while searching for word = ab is = indexR2L is =
for string = fanp indexL2R while searching for word = ab is = indexR2L is =
for string = dbab indexL2R while searching for word = ab is = 3 indexR2L is = 2
Ziyad Azouny
Ziyad Azouny le 6 Mar 2020
Thank you very much for the answer :)

Connectez-vous pour commenter.

Plus de réponses (1)

Stephen23
Stephen23 le 3 Mar 2020
>> mat = ['erduopiece';'tprometall';'isdpstubaa';'gepomsicmv';'steiuratea';'otnncbcbrg';'uefctelule';'vliorbroie';'elenuuirnt';'rivierejga';'aarnelrroc';'ipecarattu';'ntegaillad';'filonolate'];
>> baz = @(r,c)[r*ones(numel(c),1),c(:)];
>> vec = num2cell(1:size(mat,1));
>> fun = @(s)cell2mat(cellfun(baz,vec(:),strfind(num2cell(mat,2),s),'uni',0)).';
>> str = 'er';
>> fprintf('The word was found from left to right at the position (%u,%u)\n',fun(str))
The word was found from left to right at the position (1,1)
The word was found from left to right at the position (10,5)
>> fprintf('The word was found from right to left at the position (%u,%u)\n',fun(fliplr(str)))
The word was found from right to left at the position (10,6)

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by