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 件のコメント
Mil Shastri
2020 年 2 月 19 日
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
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
2020 年 3 月 3 日
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 件のコメント
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
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)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!