How to replace a number with 0 in an array

51 vues (au cours des 30 derniers jours)
Lilja Dahl
Lilja Dahl le 12 Fév 2016
Modifié(e) : dpb le 7 Sep 2020
How to replace a number in a column with 0? I want to replace all 0.5 values in a column with 0

Réponse acceptée

dpb
dpb le 12 Fév 2016
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 commentaires
Lilja Dahl
Lilja Dahl le 12 Fév 2016
Modifié(e) : Lilja Dahl le 12 Fév 2016
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 le 12 Fév 2016
Modifié(e) : dpb le 12 Fév 2016
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Jos (10584)
Jos (10584) le 12 Fév 2016
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 commentaires
vanam sindhuja
vanam sindhuja le 7 Sep 2020
Can I know the significance of tf.
dpb
dpb le 7 Sep 2020
Modifié(e) : dpb le 7 Sep 2020
...
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by