Remove '00' the first two digit to '0' in cell

5 Ansichten (letzte 30 Tage)
eko supriyadi
eko supriyadi am 12 Jan. 2023
Kommentiert: Walter Roberson am 12 Jan. 2023
Hi community,
Suppose i have array in cell:
ab={'10300' '20257' '30073' '40080' '55011' '82132' '0' '0' '0'; '10321' '20258' '30084' '40091' '56024' '00822' '0' '0' '0'};
the question how change '00822' that contain '00' the first two digit to '0', so the result i want is:
ab={'10300' '20257' '30073' '40080' '55011' '82132' '0' '0' '0'; '10321' '20258' '30084' '40091' '56024' '0' '0' '0' '0'};
thx

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Jan. 2023
Are you sure you do not want to remove all leading zeros (leaving, of course a lone 0) ?
ab={'10300' '020257' '30073' '00080' '55011' '82132' '0' '00' '0'; '10321' '20258' '30084' '40091' '56024' '00822' '0' '0' '0'}
ab = 2×9 cell array
{'10300'} {'020257'} {'30073'} {'00080'} {'55011'} {'82132'} {'0'} {'00'} {'0'} {'10321'} {'20258' } {'30084'} {'40091'} {'56024'} {'00822'} {'0'} {'0' } {'0'}
ab_remove_00_only = regexprep(ab, '^00(?=\d)', '')
ab_remove_00_only = 2×9 cell array
{'10300'} {'020257'} {'30073'} {'080' } {'55011'} {'82132'} {'0'} {'00'} {'0'} {'10321'} {'20258' } {'30084'} {'40091'} {'56024'} {'822' } {'0'} {'0' } {'0'}
ab_remove_all_leading_0 = regexprep(ab, '^0+(?=\d)', '')
ab_remove_all_leading_0 = 2×9 cell array
{'10300'} {'20257'} {'30073'} {'80' } {'55011'} {'82132'} {'0'} {'0'} {'0'} {'10321'} {'20258'} {'30084'} {'40091'} {'56024'} {'822' } {'0'} {'0'} {'0'}
  1 Kommentar
Walter Roberson
Walter Roberson am 12 Jan. 2023
After re-reading the question:
ab={'10300' '020257' '30073' '00080' '55011' '82132' '0' '00' '0'; '10321' '20258' '30084' '40091' '56024' '00822' '0' '0' '0'}
ab = 2×9 cell array
{'10300'} {'020257'} {'30073'} {'00080'} {'55011'} {'82132'} {'0'} {'00'} {'0'} {'10321'} {'20258' } {'30084'} {'40091'} {'56024'} {'00822'} {'0'} {'0' } {'0'}
ab_zap_00 = regexprep(ab, '^00.*', '0')
ab_zap_00 = 2×9 cell array
{'10300'} {'020257'} {'30073'} {'0' } {'55011'} {'82132'} {'0'} {'0'} {'0'} {'10321'} {'20258' } {'30084'} {'40091'} {'56024'} {'0' } {'0'} {'0'} {'0'}

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Karim
Karim am 12 Jan. 2023
Hi, you can do that with the startswith function of matlab:
ab = {'10300' '20257' '30073' '40080' '55011' '82132' '0' '0' '0'; '10321' '20258' '30084' '40091' '56024' '00822' '0' '0' '0'}
ab = 2×9 cell array
{'10300'} {'20257'} {'30073'} {'40080'} {'55011'} {'82132'} {'0'} {'0'} {'0'} {'10321'} {'20258'} {'30084'} {'40091'} {'56024'} {'00822'} {'0'} {'0'} {'0'}
% use the 'starts with' function to determine the locations
TF = startsWith(ab,"00")
TF = 2×9 logical array
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
% replace with a single '0'
ab(TF) = {'0'}
ab = 2×9 cell array
{'10300'} {'20257'} {'30073'} {'40080'} {'55011'} {'82132'} {'0'} {'0'} {'0'} {'10321'} {'20258'} {'30084'} {'40091'} {'56024'} {'0' } {'0'} {'0'} {'0'}

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by