MATLAB符號用法(1)
>> a=rand(3,4)
a =
0.8147 0.9134 0.2785 0.9649
0.9058 0.6324 0.5469 0.1576
0.1270 0.0975 0.9575 0.9706
1. 數位轉換字串
>> a1=num2str(a,3)
a1 =
0.815 0.913 0.278 0.965
0.906 0.632 0.547 0.158
0.127 0.0975 0.958 0.971
2. 字串轉換為整數
>> a2=str2num(a1)
a2 =
0.8150 0.9130 0.2780 0.9650
0.9060 0.6320 0.5470 0.1580
0.1270 0.0975 0.9580 0.9710
3. 整數轉換字串
>> a3='123456'
a3 =
123456
>> a4=int2str(a3)
a4 =
49 50 51 52 53 54
4. 矩陣轉換為字元
>> a5=mat2str(a,3)
a5 =
[0.815 0.913 0.278 0.965;0.906 0.632 0.547 0.158;0.127 0.0975 0.958 0.971]
5. 字串轉換為矩陣
>> a6='one',a7='two'
a6 =
one
a7 =
two
>> a8=str2mat(a6,a7,'three')
a8 =
one
two
three
6. 連接串
>> z=char('h','o','w','','a','r','e','','y','o','u')
z =
h
o
w
a
r
e
y
o
u
>> z1=strcat(z)
z1 =
h
o
w
a
r
e
y
o
u
7. 垂直連接串
>> s1='i',s2='love',s3='banana'
s1 =
i
s2 =
love
s3 =
banana
>> strvcat(s1,s2,s3)
ans =
i
love
banana
8. 串比較
>> n1='abc',n2='abcd',n3='abc'
n1 =
abc
n2 =
abcd
n3 =
abc
>> strcmp(n1,n2)
ans =
0
>> strcmp(n1,n3)
ans =
1
9. 比較串前n個字元
>> g1='abcdeg',g2='abcdef'
g1 =
abcdeg
g2 =
abcdef
>> strncmp(g1,g2,3)
ans =
1
>> strncmp(g1,g2,6)
ans =
0
10. 在其他串中尋找此串
>> y='i love apple and banana'
y =
i love apple and banana
>> findstr(y,' ')
ans =
2 7 13 17
11. 串對齊
>> m=char('3','46','754')
m =
3
46
754
>> strjust(m,'center')
ans =
3
46
754
>> strjust(m,'right')
ans =
3
46
754
12. 查找匹配的字串
>> strmatch('x',strvcat('u','v','w','x','y','z'))
ans =
4
13. 串替代
>> d='mary is beautiful'
d =
mary is beautiful
>> strrep(d,'m','M')
ans =
Mary is beautiful
14. 尋找串中的記號
>> h='you are so great'
h =
you are so great
>> strtok(h)
ans =
you
15. 大寫串
>> i='i need your help'
i =
i need your help
>> upper(i)
ans =
I NEED YOUR HELP
16. 小寫串
>> u='VERY GOOD'
u =
VERY GOOD
>> lower(u)
ans =
very good
17. 空格串
>> l=['i',blanks(2),'miss',blanks(2),'you']
l =
i miss you
18. 刪掉串中空格
>> o='a b c d '
o =
a b c d
>> deblank(o)
ans =
a b c d
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。