Matrix to string, filled with zeros
2 次查看(过去 30 天)
显示 更早的评论
I have a question about transforming a matrix. I have a matrix that looks something like this:
A = [9998; 9999; 0; 1; 2; ...... 9997; 9998; 9999; 0; 1; 2]
I want to turn this matrix into a string which contains always 4 elements:
B = ['9998'; '9999'; '0000'; '0001'; '0002'; ..... '9997'; '9998'; '9999'; '0000'; '0001'; '0002']
Then I also want to create a matrix (preferably without a loop) that adds 1 every time the number jumps down from 9999 to 0:
C = [0; 0; 1; 1; 1; 1; 1; 1; ..... 1; 1; 1; 2; 2; 2]
0 个评论
采纳的回答
dpb
2021-5-17
>> string(num2str(A,'%04d')).'
ans =
1×11 string array
"9998" "9999" "0000" "0001" "0002" "9997" "9998" "9999" "0000" "0001" "0002"
>> cumsum([0 diff(A.')<0])
ans =
0 0 1 1 1 1 1 1 2 2 2
>>
1 个评论
dpb
2021-5-17
compose was what was trying to remember was called but didn't at the time...I'd suggest a combination solution of Matt's in lieu of num2str
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!