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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by