0

単語と数字を含む text.txt があります。

1. 文字列を含むセルまたは行列に「for」と「if」を使用したい。

d = reshape(textread('text.txt','%s','delimiter','\t'),2,2)'
if h(1,1)=='apple'
    k=1
end

これはうまくいきませんでした。

2. こんなことがしたい。

for m=1:size(text)
   for n=1:size(text2)
       if text2(n,3) contains 'apple' (such as "My apple his, her")
          if last word of text2(n,3) is text(m,2)
             output(m,1)==text(m,2)
             output(m,2)==text(m,1)
          end
       end
   end
end

Matlab 以外の方法で書かれていますが、私がやりたいことはわかっています。

text と text2 が数値の行列である場合、このタイプの作業は簡単かもしれません。しかし、それらは文字列です。

数値行列に対して行うのと同じことをどのように実行できますか?

4

1 に答える 1

1

文字列操作ユーティリティを探していると思います。ここにいくつかあります:

% Case-sensitive comparison
strcmp('apple', 'apple') == true
strcmp('apple', 'orange') == false    

% Case-INsensitive comparison
strcmpi('apple', 'AppLe') == true
strcmpi('apple', 'orange') == false    

% Compare first N characters, case sensitive
strncmp('apple', 'apples are delicious', 5) == true   

% Compare first N characters, case INsensitive
strncmpi('apple', 'AppLes are delicious', 5) == true   

% find strings in other strings
strfind('I have apples and oranges', 'apple') 
ans =
    8    % match found at 8th character 

% find any kind of pattern in (a) string(s)
regexp(...) 

さて、私は始めるつもりさえありませんregexp...ただ読んでくださいdoc regexp :)

于 2013-06-13T08:04:54.223 に答える