Replacing Values in Matrix that are not equal to 1 while excluding 'NaN'

1 Ansicht (letzte 30 Tage)
zwon
zwon am 2 Aug. 2022
Kommentiert: zwon am 3 Aug. 2022
I have a globally gridded 3D matrix of air temperature where the 'z' dimension is time. I need to convert all values within the matrix to 1 or 0 depending on whether the values are above or below 0. All temperature values that would fall over water are missing and have been replaced with NaN. I use the following lines to convert the matrix to 1's and 0's, but it also converts the NaN's to 0. I don't want this to happen.
This is currently what I have:
temp_final_1(temp_final_1 > set_point) = 1;
temp_final_1(temp_final_1 ~= 1) = 0;
I tried this, but get an error: "Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values."
temp_final_1(temp_final_1 > set_point) = 1;
temp_final_1(temp_final_1 ~= 1 && ~isnan(temp_final_1)) = 0;
For example, this is what I am trying to accomplish. The actual values are much more varied than this.
Test1 is before values are changed to 0 or 1 if they are below or above 0 and Test2 shows this with the NaN's still intact
test1 =
NaN 2 9 2
-.23 NaN 3 2
-1 -1 NaN 6
-.4 -1 -.57 NaN
test2 =
NaN 1 1 1
0 NaN 1 1
0 0 NaN 1
0 0 0 NaN

Antworten (3)

Steven Lord
Steven Lord am 3 Aug. 2022
There are a couple different approaches you can use. With just one dividing level I'd probably just use two logical operations as @David Hill and @Les Beckham did. But if there were multiple levels I'd consider using discretize.
test = [
NaN 2 9 2
-.23 NaN 3 2
-1 -1 NaN 6
-.4 -1 -.57 NaN]
test = 4×4
NaN 2.0000 9.0000 2.0000 -0.2300 NaN 3.0000 2.0000 -1.0000 -1.0000 NaN 6.0000 -0.4000 -1.0000 -0.5700 NaN
Let's make:
  • anything less than -0.5 in test (or more technically, anything in the interval [-Inf, -0.5) in test) to be 1 in the result
  • anything in the interval [-0.5, 0.5) in test to be 4 in the result
  • anything in the interval [0.5, 3) in test to be 9 in the result
  • anything greater than or equal to 3 in test (in the interval [3, Inf]) to be 16 in the result
  • and everything else (the NaN values) to be NaN.
I've added spaces to resultValues below to put the replacement values below the comma separating the two ends of the intervals whose values will be replaced by those values.
intervalEdges = [-Inf, -0.5, 0.5, 3, Inf];
resultValues = [ 1, 4, 9, 16];
result = discretize(test, intervalEdges, resultValues)
result = 4×4
NaN 9 16 9 4 NaN 16 9 1 1 NaN 16 4 1 1 NaN

David Hill
David Hill am 2 Aug. 2022
test2=test1;
test2(test2==2)=1;
test2(test2<0)=0;
  3 Kommentare
David Hill
David Hill am 2 Aug. 2022
Bearbeitet: David Hill am 2 Aug. 2022
test1 =[NaN 2 9 2
-.23 NaN 3 2
-1 -1 NaN 6
-.4 -1 -.57 NaN];
test2=test1;
test2(test2<=0)=0;
test2(test2>0)=1
test2 = 4×4
NaN 1 1 1 0 NaN 1 1 0 0 NaN 1 0 0 0 NaN

Melden Sie sich an, um zu kommentieren.


Les Beckham
Les Beckham am 2 Aug. 2022
test1 = [
NaN 2 9 2
-.23 NaN 3 2
-1 -1 NaN 6
-.4 -1 -.57 NaN]
test1 = 4×4
NaN 2.0000 9.0000 2.0000 -0.2300 NaN 3.0000 2.0000 -1.0000 -1.0000 NaN 6.0000 -0.4000 -1.0000 -0.5700 NaN
test2 = test1;
test2(test1 > 0) = 1;
test2(test1 < 0) = 0
test2 = 4×4
NaN 1 1 1 0 NaN 1 1 0 0 NaN 1 0 0 0 NaN

Kategorien

Mehr zu Cell Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by