How to replace a number with 0 in an array

How to replace a number in a column with 0? I want to replace all 0.5 values in a column with 0

 채택된 답변

dpb
dpb 2016년 2월 12일

2 개 추천

nCol=3; % say, for the particular column
x(x(:,nCol)==0.5)=0; % turn to zero
NB: While 0.5 is exactly representable in floating point, exact comparison for floating point values is risky in general. See the FAQ <Why is 0.3-0.2-0.1 not equal to zero?>
For the general question look up "Using Logicals in Array Indexing" in the doc's; it's very important Matlab feature.

댓글 수: 2

Lilja Dahl
Lilja Dahl 2016년 2월 12일
편집: Lilja Dahl 2016년 2월 12일
Thanks alot. I am using data from a monitoring system and <0.5 stands for the detection limit of the instrument. It would also be nice to define <0.5 as random numbers from 0 to 0.5 (normal distribution).
What do you suggest? Thanks for your help
dpb
dpb 2016년 2월 12일
편집: dpb 2016년 2월 12일
Previous comment where you asked this below:
"Just replace the RHS of the assignment with whatever is wanted; only restriction is you'll have to have saved the logical vector to know how many elements are needed to generate the proper number for the assignment. A constant is propagated automagically across any size target; multiple assignment on RHS must be conformant in size to the target on the LHS."
As for "numbers from 0 to 0.5 (normal distribution)", those two distributions don't really coincide. The normal is unbounded and you can only set a probability of not exceeding either range by adjusting the mean and variance from which you sample. If you take the crude way out and simply truncate samples outside the range, then you are, indeed, in the range but the result isn't normal.
I don't know what the measurement is but it doesn't seem likely to me that a RNV would be the most probable result of measurements taken with a more precise instrument of a higher sensitivity for the nondetectable measurements from this particular instrument.
It also seems to me that introducing such artificial data into a dataset is risky at best and potentially extremely misleading at worst.
Just because you can do something doesn't mean you should...
ADDENDUM
I still have strong reservations about the wisdom of doing this, but...if you were to really, really, really believe this is the right thing to do, I'd think the likely thing would be to use a half-normal with mean 0.5- and (say) 3-sigma at 0.01 or somesuch. Take abs() of the samples generated. This would put the largest number at just under the 0.5 cutoff value and spread them out towards zero probably roughly as you envsion. Truncating then for any that are <0 shouldn't pollute the resulting distribution too much. Try
hist(0.5-abs(1/6*randn(1,1000)))
to visualize the result. I'd still caution that there ought to be some way kept to keep track of which are "pseudo" values rather than real, though if you decide to do something of the sort.

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

추가 답변 (1개)

Jos (10584)
Jos (10584) 2016년 2월 12일

0 개 추천

A = [1 2 3 ; 0.5 0.5 2 ; 0.5 5 6] % example data
C = A(:,1)
tf = C == 0.5
A(tf,1) = 0
% or in one line:
% A(A(:,1)==0.5,1) = 0

댓글 수: 5

Lilja Dahl
Lilja Dahl 2016년 2월 12일
I figured it out, thanks alot!
Lilja Dahl
Lilja Dahl 2016년 2월 12일
and if I want to define A(tf,1) to be random numbers from 0-500 (normal distributed) instead of 0?
dpb
dpb 2016년 2월 12일
Just replace the RHS of the assignment with whatever is wanted; only restriction is you'll have to have saved the logical vector to known how many elements are needed to generate the proper number for the assignment. A constant is propagated automagically across any size target; multiple assignment on RHS must be conformant in size to the target on the LHS.
Can I know the significance of tf.
dpb
dpb 2020년 9월 7일
편집: dpb 2020년 9월 7일
...
tf = C == 0.5
A(tf,1) = 0
...
tf is the logical addressing vector; the variable name chosen to represent true|false as indicator it is a logical array.
As a subscripting expression, MATLAB returns only the locations in the referenced array for which elements in the indexing expression return TRUE

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

카테고리

도움말 센터File Exchange에서 Variables에 대해 자세히 알아보기

질문:

2016년 2월 12일

편집:

dpb
2020년 9월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by