变换一个字符串,使大于5的数字表示为几个数字的总和

Gol*_*ach 0 string algorithm matlab

我需要相对于某个值转换字符串

2 3 4 5 6 1 5 16
Run Code Online (Sandbox Code Playgroud)

成为

2 3 4 5 0 5 1 5 0 5 5 5 1
Run Code Online (Sandbox Code Playgroud)

所以2 = 2,3 = 3,4 = 4,6 = 56 = 1,5 = 55 = 0,16 = 5 5 5 1.

在这种情况下,我改变了相对于的线5.我现在不知道该怎么做,所以,还没有代码.

Lui*_*ndo 6

您可以使用正则表达式replacement(regexprep).您需要负面的lookbehind以避免将6in 16作为单个检测6.

x = '2 3 4 5 6 1 5 16';
y = regexprep(x, {'(?<!\d)5' '(?<!\d)6' '16'}, {'5 0' '5 1' '5 5 5 1'});
Run Code Online (Sandbox Code Playgroud)

这给了

y =
2 3 4 5 0 5 1 1 5 0 5 5 5 1
Run Code Online (Sandbox Code Playgroud)