Write a function catit1 that will receive one input argument, which is a cell array. If the cell array contains only strings, it will return two strings : one string is all of the strings from the cell array concatenated together, the other string is
显示 更早的评论
Question: Write a function catit1 that will receive one input argument, which is a cell array. If the cell array contains only strings, it will return two strings : one string is all of the strings from the cell array concatenated together, the other string is combing the last alphabets of each word in cell array. Otherwise, it will return two empty strings.
>>fishies={‘tuna’, ‘shark’, ‘salmon’}
>>[str1,str2]=catit1(fishies)
str1= ‘tunasharksalmon’
str2= ‘akn’
I am having trouble figuring out how to get the strings to both concatenate and encrypt. Without the str2 input at the bottom, the str1 concatenation works fine.
fishies = {'tuna','shark','salmon'};
[str1,str2] = catit1(fishies);
function [str1,str2] = catit1(ca)
str1 = '';
if iscellstr(ca)
for i = 1:length(ca)
str1 = strcat(str1,ca{i});
end
end
car = char(ca);
rest = strtrim(car);
str2 = '';
while ~isempty(rest)
[word,rest] = strtok(rest);
str2 = strcat(str2,word(end));
end
end
回答(1 个)
Stephen23
2019-4-4
Simpler without loops:
>> C = {'tuna','shark','salmon'};
>> horzcat(C{:}) % comma-separated list
ans = tunasharksalmon
>> cellfun(@(v)v(end),C)
ans = akn
Read more about how this works:
类别
在 帮助中心 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!