How to replace numbers in an array

3 ビュー (過去 30 日間)
Adnanjo
Adnanjo 2018 年 11 月 14 日
編集済み: madhan ravi 2018 年 11 月 14 日
Hello,
I have this vector A = [000000100000020000003]
I need to replace all zeros with the same values the comes each after zero, so the matrix will be
B = [111111111122222222222333333333333]
how to do this in Matlab ?
Regards
  1 件のコメント
Guillaume
Guillaume 2018 年 11 月 14 日
It is important to use correct notation.
"I have this vector A = [000000100000020000003]"
[000000100000020000003] is not a vector, it's a scalar. The [] do nothing at all here, and the above is equivalent to:
A = 100000020000003
Similarly, B = [111111111122222222222333333333333] is the same as
B = 111111111122222222222333333333333
However, because of that number is much larger than flintmax it can't be stored accurately as a double (or even as uint64), and will be stored as the nearest double:
B = 111111111122222215928577118961664 %closest double value to 111111111122222222222333333333333
Hopefully, your A is actually
A = [0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 3]
as per Madhan's answer.

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

回答 (1 件)

madhan ravi
madhan ravi 2018 年 11 月 14 日
編集済み: madhan ravi 2018 年 11 月 14 日
A = [0 0 0 0 0 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 3]
A(A==0)=NaN;
B=fillmissing(A,'NEXT')
command window:
>> COMMUNITY
A =
Columns 1 through 13
0 0 0 0 0 0 1 0 0 0 0 0 0
Columns 14 through 21
2 0 0 0 0 0 0 3
B =
Columns 1 through 13
1 1 1 1 1 1 1 2 2 2 2 2 2
Columns 14 through 21
2 3 3 3 3 3 3 3
>>

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by