当前位置:网站首页>MATLAB data type - character type
MATLAB data type - character type
2022-06-27 00:26:00 【Fanyi】
matlab Series articles : Catalog

〇、 summary
For Mathematics , Character data may not be very important , But it is necessary when it is used . Therefore, we have this introduction to character data .
ad locum , Character data is divided into character string and Character variables To introduce , The usage of the two is slightly different .
character : and Python、Java And other high-level language types , Focused on mathematical calculations matlab There are also character data in , like a、b Data with only one character .
character string : Simply speaking , A string is data composed of several characters , such as ab、adccsa etc. .
One 、 Creation of characters and strings
1. Character creation
(1) Character creation
① Create with single quotation marks
>> a = 'abcd'
a =
'abcd'
>>
>> whos
Name Size Bytes Class Attributes
a 1x4 8 char
② use char Function creation
>> b = char('abcasd')
b =
'abcasd'
>>
>> whos
Name Size Bytes Class Attributes
b 1x6 12 char
(2) Creation of character array
① The character array uses char Function creation
Character arrays also use char() Function creation .
>> a1 = char('abcasd','asdass')
a1 =
2×6 char Array
'abcasd'
'asdass'
>>
>> whos
Name Size Bytes Class Attributes
a1 2x6 24 char
2. String creation
(1) String creation
① Create... In double quotes
>> c = "hello" % " Create string
c =
"hello"
>>
>> whos
Name Size Bytes Class Attributes
c 1x1 150 string
② use string Function creation
>> d = "hello world" % string() Function to create a string
d =
"hello world"
>>
>> whos
Name Size Bytes Class Attributes
d 1x1 166 string
(2) Creation of character array
① The character array uses [] establish
>> c1 = ["hello" "world";"nihao" "hhh"] % Be careful , Vectors of different dimensions are represented by ; separate
c1 =
2×2 string Array
"hello" "world"
"nihao" "hhh"
>>
>> whos
Name Size Bytes Class Attributes
c1 2x2 312 string
Two 、 character And character string Related operations of
Although it is about character (char) And character string (string) Related operations of , But compared to character string (string) Come on , character (char) More use of , So here's the character (char) More .
1. Length of string
There are two commonly used functions to detect the length of a string —— length() And size(), But the situation is slightly different mainly for multiple latitudes .
length()Is to select the maximum value from multiple dimensions and returnsize()Is to return the length in multiple dimensions as a vector
① length function
>> a = 'abcdef'
a =
'abcdef'
>>
>> length(a)
ans =
6
② size function
>> a = 'abcdef'
a =
'abcdef'
>>
>> size(a)
ans =
1 6
2. String splicing
character (char) And character string (string) The splicing method is slightly different .
- character (
char) adoptstrcat()Function - character string (
string) Directly through+that will do
① String splicing
character (char) adopt strcat() Function .
>> x = 'abcde'
x =
'abcde'
>>
>> y = 'fghij'
y =
'fghij'
>>
>> z = strcat(x,y)
z =
'abcdefghij'
② String splicing
character string (string) Directly through + that will do .
>> a = "abc"
a =
"abc"
>>
>> b = "def"
b =
"def"
>>
>> c = a + b
c =
"abcdef"
3. String comparison
① String comparison
adopt strcmp() Function can compare two character or character string , When two characters are identical , The return value is 1; If different , The return value is 0.
- Format :
strcmp(s1,s2),s1 And s2 For two strings that need to be matched .
>> a = 'abcd'
a =
'abcd'
>>
>> b = 'abcd'
b =
'abcd'
>>
>> strcmp(a,b)
ans =
logical
1
4. Find string
① Find string
If we want to find a short string in a long string , You can use findstr() Function to find .
findstr()The function will find the substring in a string , If found, the starting position of each substring in the long string will be returned in the form of an array . If not found, an empty array will be returned .- Format :
findstr(s_long,s_short),s_longIs a long string ,s_shortIs a short string ( Substring )
>> a = 'I am a good boy'
a =
'I am a good boy'
>>
>> findstr(a,'o')
ans =
9 10 14
5. display string
disp()Is a function used to display strings , It's similar to Java、Python In languageprint()
>> a = 'I am a good boy'
a =
'I am a good boy'
>>
>> disp(a)
I am a good boy
6. Index of characters
Character indexing is also an important function : Characters can be indexed , The string cannot
① Index character
>> a = 'abcdef' %% character
a =
'abcdef'
>>
>> for i = 1:3 %% Index character
a(i)
end
ans =
'a'
ans =
'b'
ans =
'c'
② Index string
>> b = "abcdef" %% character string
b =
"abcdef"
>>
>> for i = 1:3 %% Index string
b(i)
end
ans =
"abcdef"
Index exceeds the number of array elements. Index must not exceed 1.
An exception occurs in the index string .

边栏推荐
- 技术干货|极速、极智、极简的昇思MindSpore Lite:助力华为Watch更加智能
- 基于SSMP的宠物医院管理系统
- Special topic II on mathematical physics of the sprint strong foundation program
- [vscode] setting sync, a plug-in for synchronizing extensions and settings
- PHP代码审计系列(一) 基础:方法、思路、流程
- 手机炒股靠谱吗 网上开户炒股安全吗
- 能在手机上开户炒股吗 网上开户炒股安全吗
- In the Internet industry, there are many certificates with high gold content. How many do you have?
- Is it safe for CITIC Securities Commission to open an online account and speculate in stocks
- Kubeadm create kubernetes cluster
猜你喜欢

大咖讲 | 最前沿的昇思MindSpore开源社区运营的经验分享,快拿出小本本记录呀!

剑指 Offer 10- II. 青蛙跳台阶问题

大赛报名 | AI+科学计算重点赛事之一——中国开源科学软件创意大赛,角逐十万奖金!

入侵痕迹清理

新型冠状病毒变异Delta毒株的模拟(MindSPONGE应用)

邮箱附件钓鱼常用技法

How to easily describe the process of machine learning?

Common techniques of email attachment phishing

Concepts de base de données Oracle

Special topic II on mathematical physics of the sprint strong foundation program
随机推荐
在线上买养老年金险正规安全吗?有没有保单?
find_ Detailed use guide of CIRC
手机炒股靠谱吗 网上开户炒股安全吗
能在手机上开户炒股吗 网上开户炒股安全吗
低佣金免费开户渠道安全吗?
Leetcode 718. 最长重复子数组(暴力枚举,待解决)
冲刺强基计划数学物理专题二
Can't write to avoid killing and can easily go online CS through defender
The fourth bullet of redis interview eight part essay (end)
PHP代码审计系列(一) 基础:方法、思路、流程
05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?
墨者学院-X-Forwarded-For注入漏洞实战
com. fasterxml. jackson. databind. exc.MismatchedInputException: Expected array or string. at [Source:x
Pinpoint attackers with burp
50 tips that unity beginners can definitely use
互联网行业,常见含金量高的证书,看看你有几个?
手机能开户炒股吗 网上开户炒股安全吗
當Transformer遇見偏微分方程求解
目标追踪拍摄?目标遮挡拍摄?拥有19亿安装量的花瓣app,究竟有什么别出心裁的功能如此吸引用户?
不会写免杀也能轻松过defender上线CS