Return character array with increasing size based on input
显示 更早的评论
I'm trying to make a cell-array that increases in length and has the previous characters in the following cells + the next letter in the alphabet
the inputs are supposed to be a starting letter and a number that indicates the amounts of cells(and then also the amount of letters in the last cell)
k = input('write startingletter and then number:')
k = a3
g = str2num(k(2:end))
inncell{1,1} = k(1);
inncell{1,2} = g;
outcell = makecell(inncell) %the function makecell
outcell=
{'a'} {'ab'} {'abc'}...
this is the output I need
and this is what I've come up with, but it's not working
% a = 97, z = 122 ASCII
k = input('gimme one letter then one pos. number(exs: a5):','s');
g = str2num(k(2:end));
while isletter(k(1))~= 1 || isempty(g) || g <= 0
%errorcheck if invalid input
disp('----ERROR! input ONE letter THEN ONE POSITIVE number');
k = input('gimme one letter then one pos. number(exs: a5):','s');
g = str2num(k(2:end));
end
inncell{1,1} = k(1);
inncell{1,2} = g;
outcell = makecell(inncell);
function outcell = makecell(inncell)
n = inncell{2};
outcell = cell(1,n);
for i = 1:n
for k = 1:i
if k == 1
outcell{1, i} = char(double(inncell{1}));
else
outcell{1, i} = strcat(char(double(inncell{1}), char(double(inncell{1})+ k-1)));
end
end
end
end
3 个评论
Walter Roberson
2019-3-23
There is a one line solution once you know the starting letter and length ;-)
Nikolai
2019-3-23
Walter Roberson
2019-3-23
It is not one function, it is a single expression.
Hint:
10 11 12 13 14
10 11 12 13 14
10 11 12 13 14
10 11 12 13 14
10 11 12 13 14
-->
10 0 0 0 0
10 11 0 0 0
10 11 12 0 0
10 11 12 13 0
10 11 12 13 14
looks like a triangle of entries.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Data Preprocessing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!