How to replace numbers in an array
3 ビュー (過去 30 日間)
古いコメントを表示
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
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
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
>>
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!