# How can I use a logical array to remove and replace values in a corresponding data array?

29 visualizzazioni (ultimi 30 giorni)
Steve Francis il 17 Nov 2021
Commentato: Matt J il 17 Nov 2021
I have an array A (1280 x 1024) that has some rogue values that I want to remove and replace with a linear interpolation of the two nearest neighbouring elements (that are not rogue) in the column. (I call them 'rogue' rather than outliers because the values can appear ok statistically.)
I have a logical array TF (1280 x 1024) whose elements are true if the corresponding element in A is rogue.
How do I do create the corrected array, B?
Example (5 x 3 array shown for simplicity):
TF = 5×3 logical array
0 0 0
0 0 0
0 0 1
0 1 0
0 0 0
A = 5×3
54 46 32
48 60 50
50 51 59
51 49 48
45 55 66
I want the output to be:
B = 5×3
54 46 32
48 60 50
50 51 49
51 53 48
45 55 66
The function 'filloutliers(A,'linear')' is close but that function seems to generate its own TF array whereas I want to implement the TF array that I already have.
I'm using the R2020a version of Matlab. Thank you.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Star Strider il 17 Nov 2021
See if the fillmissing funciton produces the desired result —
TF = [0 0 0
0 0 0
0 0 1
0 1 0
0 0 0];
TF = TF == 1; % Create Logical Array From Numeric Array
A = [54 46 32
48 60 50
50 51 59
51 49 48
45 55 66];
A(TF) = NaN; % Set 'TF' Entries To 'NaN'
A = fillmissing(A, 'nearest')
A = 5×3
54 46 32 48 60 50 50 51 48 51 55 48 45 55 66
.
##### 2 CommentiMostra NessunoNascondi Nessuno
Steve Francis il 17 Nov 2021
Thanks for your answer, Star Strider. This works if I replace 'nearest' with Matt's suggestion of 'linear'.
Appreciate the comments in the code.
Star Strider il 17 Nov 2021
As always, my pleasure!
.

Accedi per commentare.

### Più risposte (1)

Matt J il 17 Nov 2021
Modificato: Matt J il 17 Nov 2021
Perhaps as follows,
A(TF)=nan;
A=fillmissing(A,'linear');
##### 2 CommentiMostra NessunoNascondi Nessuno
Steve Francis il 17 Nov 2021
Matt J il 17 Nov 2021
You're welcome, but please Accept-click the answer to indicate that it worked.

Accedi per commentare.

### Categorie

Scopri di più su Image Processing and Computer Vision in Help Center e File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!

Translated by