How to replace numbers in an array
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
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.
Antworten (1)
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
>>
Note: The alignment is varied but it gives the exact result you want , see https://www.mathworks.com/help/matlab/release-notes.html?rntext=fillmissing&startrelease=R2012b&endrelease=R2018b&groupby=release&sortby=descending&searchHighlight=fillmissing released in 2016b.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!