# How to remove both leading and trailing zeros from a binary string?

조회 수: 16(최근 30일)
Noor Bano 2021년 4월 30일
댓글: Noor Bano 2021년 4월 30일
Let
A=[00000001111; 00000110010; 10111011100; 00001000110; 11111010000; 00000001010]
I want to remove zeroes from both left and right side of each binary representation. That is
Ans=[1111; 11001; 101110111; 100011; 1111101; 101]
I have tried 'short format g'.

댓글을 달려면 로그인하십시오.

### 채택된 답변

per isakson 2021년 4월 30일
편집: per isakson 2021년 4월 30일
Is this what you look for?
%%
A=["00000001111";"00000110010";"10111011100";"00001000110";"11111010000";"00000001010"];
%%
B = regexprep( A, "^[0]+", "" );
B = regexprep( B, "[0]+\$", "" )
B = 6×1 string array
"1111" "11001" "101110111" "100011" "1111101" "101"
Notice that dec2bin() returns a character row, not double
dec2bin( 123, 16 )
ans = '0000000001111011'
##### 댓글 수: 1표시숨기기 없음
Noor Bano 2021년 4월 30일
Thank you very much.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by