How to replace numbers in an array

3 Ansichten (letzte 30 Tage)
Adnanjo
Adnanjo am 14 Nov. 2018
Bearbeitet: madhan ravi am 14 Nov. 2018
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 Kommentar
Guillaume
Guillaume am 14 Nov. 2018
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

madhan ravi
madhan ravi am 14 Nov. 2018
Bearbeitet: madhan ravi am 14 Nov. 2018
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
>>

Community Treasure Hunt

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

Start Hunting!

Translated by