将相同的字符串加在一起MATLAB

问题描述

因此,我试图将两个或多个字符串汇总在一起;可以说:计算每个宠物吃掉面包的次数:“狗”,“猫”,“兔子”。如果字符串“ Dog”出现多次,并且我想将它们加起来,该怎么办?即。 '狗'+'狗'= 2

解决方法

这可能是一个选择。通过进一步的实施细节,我们可以使答案更准确地适合您的用例。这里的函数acl internal { "10.10.10.10" } if ((std.ip(req.http.X-Forward.For,"0.0.0.0") ~ internal)){ # THIS CODE } else { # OR THIS CODE } 用于评估字符串count()在声明为Dog,Cat and Bunny的字符串中出现了多少次,该次数概括了进食的次数。第二种实现涉及使用符号函数,该函数可以直接添加Bread_Eaten(符号变量)。此方法将导致输出在每个术语的末尾(例如syms处串联了Dog,Cat and Bunny

方法1:使用2*Dog函数计算字符串

count()

Output Results 1

方法2:使用一组符号变量

Bread_Eaten = "Dog" + "Dog" + "Cat" + "Bunny";

Amount_Dog_Ate = count(Bread_Eaten,"Dog");
Amount_Cat_Ate = count(Bread_Eaten,"Cat");
Amount_Bunny_Ate = count(Bread_Eaten,"Bunny");

fprintf("The dog ate bread %d time(s)\n",Amount_Dog_Ate);
fprintf("The cat ate bread %d time(s)\n",Amount_Cat_Ate);
fprintf("The bunny ate bread %d time(s)\n",Amount_Bunny_Ate);

Output Results 2

使用MATLAB R2019b运行