简体   繁体   English

如何编写一个名为kellen的MATLAB函数,该函数需要三个名为trs,ch1,ch2的输入参数?

[英]How can I write a MATLAB function named kellen that takes three input arguments named trs, ch1,ch2?

Problem description: 问题描述:

trs takes a row or column vector of string, and two characters as ch1 and ch2 . trs接受字符串的行或列向量,以及两个字符ch1ch2 If ch1 is matched with any element in trs then that element of trs will be replaced by ch2 . 如果ch1是与任何元素匹配trs则该元素trs将被替换为ch2 If ch1 is not in the vector, trs will be returned as unchanged. 如果ch1不在向量中,则trs将保持不变。

A simple example: 一个简单的例子:

Input: ww = kellen({'YOYO', 'YOYO'},'Y','X') , 输入: ww = kellen({'YOYO', 'YOYO'},'Y','X')
output: ww = {'XOXO','XOXO'} 输出: ww = {'XOXO','XOXO'}

I assume strrep function could make this problem easier but I would like to know the very basic level how MATLAB can handle this problem without using strrep function. 我认为strrep函数可以使此问题更容易,但是我想从最基本的层面上了解MATLAB如何在不使用strrep函数的情况下处理该问题。 So I request you guys please correct my code without using strrep function. 因此,我要求你们在不使用strrep函数的情况下更正我的代码。

I am new to MATLAB. 我是MATLAB新手。 Indeed, I am new to programming too. 确实,我也是编程新手。 I know I had to learn C first for basic but I did not that why is I am to struggle. 我知道我必须首先学习C的基础知识,但我没有那为什么要奋斗。

Here is my code but it seems do not work. 这是我的代码,但似乎不起作用。

function ww = kellen(trs,ch1,ch2)


[r c] = size(trs);
if r == 1 && c > 1
   for i = 1:c
       ind = trs{i} == ch1;
       trs{1,i}(ind==1) = ch2;
       ww = trs;
   end
if r == 1 && c ==1
   for i = 1:c
       ind = trs{i} == ch1;
       trs{1,i}(ind==1) = ch2;
       ww = trs
   end
end

My code works fine when size of string vector is row vector but my function is not working fine when i pass scalar of trs string. 当字符串向量的大小为行向量时,我的代码工作正常,但是当我传递trs字符串的标量时,我的函数无法正常工作。 For instance: 例如:

kellen({ 'peter piper picked a peck of pickled peppers' }, 'p', 'b')

Which part of my code should i modify? 我应该修改代码的哪一部分?

for i = 1:c
    if trs{i} == 'c1'
       outp = [trs 'c2'];
    else
       return
    end
end

The first problem I see with your code is the line: if trs{i} == 'c1' 我在您的代码中看到的第一个问题是该行: if trs{i} == 'c1'

There are a lot of problems here: 这里有很多问题:

  1. By putting '...' quotes around ch1 you are making ch1 a string literal, NOT the variable for character one that was passed into your function. 通过在ch1周围加上'...'引号,可以使ch1成为字符串文字,而不是传递给函数的第一个字符的变量。 Drop the quotes. 删除引号。 This is the source of your error, attempting to equate a 4 character string with a 3 character string. 这是您尝试将4字符串与3字符串等效的错误的根源。
  2. You are equating a whole string trs{i} with a single character ch1 . 您将整个字符串trs{i}等同于单个字符ch1 You can do that in Matlab, but NOT in an if statement. 您可以在Matlab中执行此操作,但不能在if语句中执行。 Let's take your example inputs, trs{1} is 'YOYO' and if we try 'YOYO'=='Y' we get a logical vector like [1 0 1 0]. 让我们以您的示例输入为例, trs{1}'YOYO' ,如果尝试'YOYO'=='Y'我们将得到类似[1 0 1 0]的逻辑矢量。 Now if expects just a 1 or 0 and not a vector. 现在, if只期望10而不是向量。

So I would suggest dropping the if statement and taking advantage of Matlab's logical indexing instead: 因此,我建议删除if语句并改用Matlab的逻辑索引:

outp{c} = [];  %// this is just preallocation
for i = 1:c
    temp = trs{i};
    idx = temp==ch1 %// Get a logical matrix of which characters match
    temp(idx)=ch2; %// Use logical indexing to replace all the characters that match in this string in one go!
    outp{i} = temp;
end

Once you understand this code you can simplify: 了解了此代码后,您可以简化:

outp = trs;
for i = 1:c
    outp{i}(outp{i}==ch1)=ch2
end

You can use ismember() to find the locations where ch1 is present in trs . 您可以使用ismember()查找trs存在ch1的位置。 So, in fact trs(ismember(trs,ch1)) = ch2; 因此,实际上trs(ismember(trs,ch1)) = ch2; will replace all instances of ch1 with ch2 in trs . 将在trs中用ch2替换ch1所有实例。

However, you need to make sure that ch1 and ch2 are exactly one characters long. 但是,您需要确保ch1ch2长度恰好是一个字符。 If you want to have trs as a cell of strings, like in your question, than you can either loop through it the way you do now, or you can take a look at the cellfun() function. 如果您想将trs作为字符串单元格(如您的问题中所述),则可以按现在的方式循环遍历它,也可以看看cellfun()函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何编写名为dedbi的MATLAB函数,该函数将输入xtx作为字符串并返回另一个字符串xtxx作为输出。 - How can i write a MATLAB function named dedbi that takes input xtx as string and returns another string xtxx as output. 我怎么能找到这个名为“svmpredict.m”的matlab函数 - how can I find this matlab function named “svmpredict.m” Matlab)如何用各种输入参数绘制函数图? - Matlab) how can I plot graphs of function with various input arguments? 如何在 octave 中使用 matlab 表(带有命名变量的数组) - How can I use matlab table (array with named variables) in octave 如何调用从命令行输入文本的Matlab函数? - How can I call a Matlab function that takes text input from the command line? 编写一个函数,将输入 `n` 作为输入并返回矩阵 C,如果 i/j&lt;2,则 Cij=0,否则在 MATLAB 中 Cij =ij^2 - Write a function that takes as input `n` and returns the matrix C, Cij= 0 if i/j<2 , Cij =ij^2 otherwise in MATLAB 如何将向量传递给相同大小的函数句柄,以便它自动将第一个输入作为第一个变量等等? MATLAB - How can I pass a vector to a function handle of the same size , so that it automatically takes the first input as the first variable and so on? MATLAB 如何使用带有Matlab系统功能的Python处理“没有名为library_name的模块”错误? - How do I deal with 'no module named library_name' error using Python with Matlab's system function? 如何将Matlab结构解压缩为函数参数? - How can I unpack a Matlab structure into function arguments? 在MATLAB中,如何方便地为函数提供许多参数? - In MATLAB, how can I conveniently supply many arguments to a function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM