我如何编写一个名为 dedbi 的 MATLAB 函数,它将输入 xtx 作为字符串并返回另一个字符串 xtxx 作为输出。

2024-05-15

dedbi 反转单词,即“a”将被“z”替换,“b”将被“y”替换,“c”将被“x”替换,依此类推。 dedbi 将对大写字母执行相同的操作,即将字符串“A”替换为“Z”,将“B”替换为“Y”,将“C”替换为“X”,依此类推。如果我给函数这个字符串'ab AB'函数应该返回'zy ZY',输入除英语单词之外的输入它将返回输入作为输出,输入为'///\?'将返回输出为'///\? '。

到目前为止我写了这段代码。我必须承认这个问题来自我需要通过的作业。 谢谢你们友善的目光。

function xtxx = dedbi(xtx)
txtt = char(xtx);
indexa = strfind(txtt,'a');
txtt(indexa) = 'z';
indA = strfind(txtt,'A');
txtt(indA) = 'Z';
indb = strfind(txtt,'b');
txtt(indb) = 'y';
indB = strfind(txtt,'B');
txtt(indB) = 'Y';
indc = strfind(txtt,'c');
txtt(indc) = 'x';
indC = strfind(txtt,'C');
txtt(indC) = 'X';
indd = strfind(txtt,'d');
txtt(indd) = 'w';
indD = strfind(txtt,'D');
txtt(indD) = 'W';
inde = strfind(txtt,'e');
txtt(inde) = 'v';
indE = strfind(txtt,'E');
txtt(indE) = 'V';
indf = strfind(txtt,'f');
txtt(indf) = 'u';
indF = strfind(txtt,'F');
txtt(indF) = 'U';
indg = strfind(txtt,'g');
txtt(indg) = 't';
indG = strfind(txtt,'G');
txtt(indG) = 'T';
indh = strfind(txtt,'h');
txtt(indh) = 's';
indH = strfind(txtt,'H');
txtt(indH) = 'S';
indi = strfind(txtt,'i');
txtt(indi) = 'r';
indI = strfind(txtt,'I');
txtt(indI) = 'R';
indj = strfind(txtt,'j');
txtt(indj) = 'q';
indJ = strfind(txtt,'J');
txtt(indJ) = 'Q';
indk = strfind(txtt,'k');
txtt(indk) = 'p';
indK = strfind(txtt,'K');
txtt(indK) = 'P';
indl = strfind(txtt,'l');
txtt(indl) = 'o';
indL = strfind(txtt,'L');
txtt(indL) = 'O';
indm = strfind(txtt,'m');
txtt(indm) = 'n';
indM = strfind(txtt,'M');
txtt(indM) = 'N';
indn = strfind(txtt,'n');
txtt(indn) = 'm';
indN = strfind(txtt,'N');
txtt(indN) = 'M';
indo = strfind(txtt,'o');
txtt(indo) = 'l';
indO = strfind(txtt,'O');
txtt(indO) = 'L';
indp = strfind(txtt,'p');
txtt(indp) = 'k';
indP = strfind(txtt,'P');
txtt(indP) = 'K';
indq = strfind(txtt,'q');
txtt(indq) = 'j';
indQ = strfind(txtt,'Q');
txtt(indQ) = 'J';
indr = strfind(txtt,'r');
txtt(indr) = 'i';
indR = strfind(txtt,'R');
txtt(indR) = 'I';
inds = strfind(txtt,'s');
txtt(inds) = 'h';
indS = strfind(txtt,'S');
txtt(indS) = 'H';
indt = strfind(txtt,'t');
txtt(indt) = 'g';
indT = strfind(txtt,'T');
txtt(indT) = 'G';
indu = strfind(txtt,'u');
txtt(indu) = 'f';
indU = strfind(txtt,'U');
txtt(indU) = 'F';
indv = strfind(txtt,'v');
indv(txtt) = 'e';
indV = strfind(txtt,'V');
txtt(indV) = 'E';
indw = strfind(txtt,'w');
txtt(indw) = 'd';
indW = strfind(txtt,'W');
txtt(indW) = 'D';
indx = strfind(txtt,'x');
txtt(indx) = 'c';
indX = strfind(txtt,'X');
txtt(indX) = 'C';
indy = strfind(txtt,'y');
txtt(indy) = 'b';
indY = strfind(txtt,'Y');
txtt(indY) = 'B';
indz = strfind(txtt,'z');
txtt(indz) = 'a';
indZ = strfind(txtt,'Z');
txtt(indZ) = 'A';

str = xtxx;
end

一种方法——

%// in_str: Input string

%// Logical masks for upper and lower case letters
upper_mask = in_str>=65 & in_str<=90  %// OR isstrprop(in_str,'upper')
lower_mask = in_str>=97 & in_str<=122 %// OR isstrprop(in_str,'lower')

%// Initialize output string
out_str = in_str

%// Replace upper letters with "flipped" upper letters; repeat for small letters
out_str(upper_mask) = char(in_str(upper_mask) + 2*(78-in_str(upper_mask))-1)
out_str(lower_mask) = char(in_str(lower_mask) + 2*(110-in_str(lower_mask))-1)

这些数字:78 and 110分别充当大写字母和小写字母范围的“中间数字”,用于查找这两个类别中每个字母的差异。

样本运行 -

>> in_str,out_str
in_str =
adzC+*6AY&‘///\?abAB
out_str =
zwaX+*6ZB&‘///\?zyZY
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我如何编写一个名为 dedbi 的 MATLAB 函数,它将输入 xtx 作为字符串并返回另一个字符串 xtxx 作为输出。 的相关文章

  • MATLAB:MEX 矩阵除法给出的结果与 m 文件不同

    我使用 MATLAB 的编码器工具创建了矩阵指数函数的 MEX 版本 以在另一组函数中使用 问题是 MEX 版本给出的结果与原始 m 文件不同 经过调试 我认为这是因为MEX文件和m文件没有做相同的矩阵除法 或者 MEX 文件首先就有问题
  • matlab 中的 for 或 while 循环

    我刚刚开始在编程课的 matlab 中使用 for 循环 基本的东西对我来说很好 但是我被要求 使用循环创建一个 3 x 5 矩阵 其中每个元素的值是其行号其列号除以行号和列号之和的幂 例如元素 2 3 的值为 2 3 2 3 1 6 那么
  • 如何在 MATLAB 的 for 循环中读取多个图像?

    我已将结果分段放在一个文件夹中 这些需要在 for 循环中读取并在循环中进一步处理 我尝试阅读如下 for i 1 10 file name dir strcat C Users adminp Desktop dinosaurs im im
  • Matlab 中的多行匿名函数? [复制]

    这个问题在这里已经有答案了 是否可以在 Matlab 中创建多行匿名函数 没有合适的例子在文档中 http www mathworks com help matlab matlab prog anonymous functions html
  • 覆盖 MATLAB 默认静态 javaclasspath 的最佳方法

    MATLAB 配置为在搜索用户可修改的动态路径之前搜索其静态 java 类路径 不幸的是 静态路径包含相当多非常旧的公共库 因此如果您尝试使用新版本 您可能最终会加载错误的实现并出现错误 例如 静态路径包含 google collectio
  • 一次分配多个字段的聪明方法?

    由于遗留函数调用 我有时被迫编写像这样的丑陋的包装器 function return someWrapper someField a someField a b someField b and so on realistically it
  • 如何在 MATLAB 中可视化球体的交集?

    似乎这个问题在一些地方被问过 包括SO https stackoverflow com questions 35130336 draws the intersecting volume of two spheres in matlab 我最
  • matlab mex 文件和 C++ dll (Windows)

    我有一个带有 Test 类的 DLL 标题 class MY EXPORT Test public int doit const string str 和来源 int Test doit const string str return in
  • 检查Matlab中脚本需要使用的函数

    我有一个别人写的代码包 我正在运行一个脚本 它调用一些函数 这些函数又调用更多函数 等等 我想获取不是 MATLAB 内置函数但属于包的一部分的函数列表 我尝试使用matlab codetools requiredFilesAndProdu
  • MATLAB 中最有效的矩阵求逆

    在 MATLAB 中计算某个方阵 A 的逆矩阵时 使用 Ai inv A should be the same as Ai A 1 MATLAB 通常会通知我这不是最有效的求逆方法 那么什么是更有效率的呢 如果我有一个方程系统 可能会使用
  • 在另一列中添加具有特定条件的一列,如 excel 的 sumif

    我有一个像这样的矩阵 A 1 2 2 3 3 4 4 5 5 6 6 8 7 9 8 5 9 4 现在我想添加第二列 条件是如果 limit 0 interval 3 且 limit limit interval 或者换句话说 当第 1 列
  • Python 函数句柄 ala Matlab

    在 MATLAB 中可以创建function handles http www mathworks co uk help techdoc ref function handle html与类似的东西 myfun arglist body 这
  • 在 matlab 中求 3d 峰的体积

    现在我有一个带有峰值的 3D 散点图 我需要找到其体积 我的数据来自图像 因此 x 和 y 值表示 xy 平面上的像素位置 z 值是每个像素的像素值 这是我的散点图 scatter3 x y z 20 z filled 我试图找到数据峰值的
  • MATLAB - 如何将子图一起缩放?

    我在一张图中有多个子图 每个图的 X 轴是相同的变量 时间 每个图上的 Y 轴都不同 无论是它所代表的内容还是数据的大小 我想要一种同时放大所有图的时间尺度的方法 理想情况下 可以在其中一张图上使用矩形缩放工具 并让其他图相应地更改其 X
  • 我需要转义该 MATLAB 字符串中的字符吗?

    我想在 MATLAB 中调用以下 bash 命令 grep Up to test linux vision1 1 log awk print 7 I use system 在MATLAB中 但结果有错误 gt gt status strin
  • matlab中类库的全局变量

    我有一些matlab声明的类 我如何声明所有类中都可见的常量 例如 这些常量可以是在所有类的方法中使用的物理常量 首先想到的是使用全局变量 还有更好的办法吗 最好在单独的文件中声明这些常量 包含常量的类是执行此操作的一种很好的干净方法 请参
  • 黑白随机着色的六角格子

    我正在尝试绘制一个 10 000 x 10 000 随机半黑半白的六边形格子 我不知道如何将该格子的六边形随机填充为黑色和白色 这是我真正想要从这段代码中得到的示例 但我无法做到 https i stack imgur com RkdCw
  • 检测植物图片中的所有分支

    我想知道有什么可以检测下图中的所有绿色树枝 目前我开始应用 Frangi 过滤器 options struct FrangiScaleRange 5 5 FrangiScaleRatio 1 FrangiBetaOne 1 FrangiBe
  • 如何使用 MATLAB 的 substruct 函数创建表示使用“end”的引用的结构?

    我想使用substruct http www mathworks com help matlab ref substruct html函数创建一个结构体以供使用subsref 目的是使用索引字符串subsref而不是通常的 符号 因为我正在
  • 理解高斯混合模型的概念

    我试图通过阅读在线资源来理解 GMM 我已经使用 K 均值实现了聚类 并且正在了解 GMM 与 K 均值的比较 以下是我的理解 如有错误请指出 GMM 类似于 KNN 在这两种情况下都实现了聚类 但在 GMM 中 每个簇都有自己独立的均值和

随机推荐