简体   繁体   中英

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 . If ch1 is matched with any element in trs then that element of trs will be replaced by ch2 . If ch1 is not in the vector, trs will be returned as unchanged.

A simple example:

Input: ww = kellen({'YOYO', 'YOYO'},'Y','X') ,
output: 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. So I request you guys please correct my code without using strrep function.

I am new to 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.

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. 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'

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. Drop the quotes. This is the source of your error, attempting to equate a 4 character string with a 3 character string.
  2. You are equating a whole string trs{i} with a single character ch1 . You can do that in Matlab, but NOT in an if statement. 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]. Now if expects just a 1 or 0 and not a vector.

So I would suggest dropping the if statement and taking advantage of Matlab's logical indexing instead:

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 . So, in fact trs(ismember(trs,ch1)) = ch2; will replace all instances of ch1 with ch2 in trs .

However, you need to make sure that ch1 and ch2 are exactly one characters long. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM