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!