I have a matrix that contains strings and i have to find words in it.
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ziyad Azouny
el 19 de Feb. de 2020
Comentada: Ziyad Azouny
el 6 de Mzo. de 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 comentarios
Mil Shastri
el 19 de Feb. de 2020
Hi Ziyad, could you rephrase your question. It's not very clear.
Also, consider checking out a previous discussion: https://www.mathworks.com/matlabcentral/answers/231706-finding-position-of-a-string-in-a-cell-array
Stephen23
el 3 de Mzo. de 2020
Editada: Stephen23
el 3 de Mzo. de 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']
Respuesta aceptada
Jalaj Gambhir
el 3 de Mzo. de 2020
Hi,
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 comentarios
Jalaj Gambhir
el 3 de Mzo. de 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
Más respuestas (1)
Stephen23
el 3 de Mzo. de 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)
0 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!