필터 지우기
필터 지우기

How to replace vector value '0' to NaN( 0 -> Nan)

조회 수: 48 (최근 30일)
Jingyu Yang
Jingyu Yang 2020년 8월 31일
댓글: Jingyu Yang 2020년 9월 6일
If my vector is like
For example ) If my vector is like "Distance = { 0 1 2 3 0 4 0 5 0 6 7 8 0 9 10 } "...
And What I want is "Distance = { Nan 1 2 3 Nan 4 Nan 5 Nan 6 7 8 Nan 9 10}";
Literally, I want to change value 0 to value Nan.
  댓글 수: 1
Stephen23
Stephen23 2020년 8월 31일
There does not seem to be any benefit here to using a cell array, your code will be simpler and much more efficient if you just use a basic numeric vector:

댓글을 달려면 로그인하십시오.

답변 (1개)

Alan Stevens
Alan Stevens 2020년 8월 31일
>> Distance = [0 1 2 3 0 4 0 5 0 6 7 8 0 9 10]
Distance =
0 1 2 3 0 4 0 5 0 6 7 8 0 9 10
>> Distance(Distance==0)=NaN
Distance =
NaN 1 2 3 NaN 4 NaN 5 NaN 6 7 8 NaN 9 10
NB Just use square brackets to define a vector, unless you really want to set it up as a series of cells.
  댓글 수: 2
Vladimir Sovkov
Vladimir Sovkov 2020년 8월 31일
If the cell array is what really wanted, use the function cell2mat to first convert it to the double array, do with it what Alan recommended, then convert it back to the cell array with the function mat2cell. In the Alan code, if you like it, you can also use Distance(~Distance)=NaN instead of Distance(Distance==0)=NaN.
Jingyu Yang
Jingyu Yang 2020년 9월 6일
How about in time table?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by