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]

 採用された回答

dpb
dpb 2021 年 5 月 17 日

0 投票

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

サインインしてコメントする。

その他の回答 (1 件)

Matt J
Matt J 2021 年 5 月 17 日
編集済み: Matt J 2021 年 5 月 17 日

2 投票

B=compose('%.4d',A);
C=cumsum( A(2:end)==0 & A(1:end-1)==9999 );

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

製品

リリース

R2020b

質問済み:

2021 年 5 月 17 日

コメント済み:

dpb
2021 年 5 月 17 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by