[英]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
接受字符串的行或列向量,以及两个字符ch1
和ch2
。 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: 这里有很多问题:
'...'
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. trs{i}
with a single character ch1
. trs{i}
等同于单个字符ch1
。 You can do that in Matlab, but NOT in an if
statement. 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
只期望1
或0
而不是向量。 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. 但是,您需要确保
ch1
和ch2
长度恰好是一个字符。 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.