How to delete leading zeros in a row vector in MATLAB?

9 ビュー (過去 30 日間)
Emily
Emily 2014 年 3 月 26 日
回答済み: Shivam010 2019 年 9 月 8 日
I have created a function which multiplies two factorials together in MATLAB and the final result is:
0000000000000000000000000000000000000000
0000000000000005127485891768928006848657
3067757467756330382900027197934928341442
4644048874750576347830749966301579885959
6864463688769208320000000000000000000000
00
I need to delete the leading zeros, without deleting the trailing zeros. Anyone know how to do this?
Would help a lot, thanks
  4 件のコメント
Walter Roberson
Walter Roberson 2014 年 3 月 26 日
In MATLAB the only way to get a number that long would be by using the Symbolic Toolbox, which would never print leading zeros.
Emily
Emily 2014 年 3 月 26 日
it is a vector, i just put it in the form of a number at the end. I need to know how to get rid of the leading zeros in vector form please?

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

採用された回答

Mischa Kim
Mischa Kim 2014 年 3 月 26 日
If it's a vector of chars, this should do:
data = regexprep(data,'^0*','')

その他の回答 (3 件)

Joseph Cheng
Joseph Cheng 2014 年 3 月 26 日
So I take it is a string array? If it is I would use something like this.
result='000000000123450101010'
mask = ones(size(result));
zero = strfind(result,'0'); %find all the zeros
mask(zero)=0; %mask off the zeros
msb = find(mask~=0); %find what numbers are not zero in the mak
result=result(msb(1):end) %first non zero number to the end of the result is the number.

Shivam010
Shivam010 2019 年 9 月 8 日
d=[0 0 0 0 0 1 0 0 4 5 0 1]
g=find(d==0)
for i=1:length(g)
if g(i)==i
d(1)=[]
end
end
output:
d =
1 0 0 4 5 0 1

Walter Roberson
Walter Roberson 2017 年 7 月 25 日
編集済み: Walter Roberson 2017 年 7 月 25 日
S = '0000000000000000000000000000000000000000000000000000000512748589176892800684865730677574677563303829000271979349283414424644048874750576347830749966301579885959686446368876920832000000000000000000000000';
result = S(find(S ~= '0', 1, 'first') : end);

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by