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

14 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2020-3-3
编辑:Stephen23 2020-3-3
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']

请先登录,再进行评论。

采纳的回答

Jalaj Gambhir
Jalaj Gambhir 2020-3-3
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 个评论
Jalaj Gambhir
Jalaj Gambhir 2020-3-3
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

请先登录,再进行评论。

更多回答(1 个)

Stephen23
Stephen23 2020-3-3
>> 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)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by