Reputation: 33
I have an array (data2winner) that looks like this:
Columns 1 through 6
1.0894 1.1923 1.0238 1.0625 1.0222 0.9701
1.0000 1.2419 1.2093 0.9412 0.9783 1.0923
1.0379 NaN 1.0000 1.0417 1.0667 1.0000
Columns 7 through 12
0.9661 0.9568 1.0400 0.8519 0.8675 0.9821
0.7895 1.0000 NaN 1.0000 1.1250 0.8182
1.0667 0.7714 NaN 1.0000 0.9383 1.1111
I want to replace all NaN in the same row (e.g. row 3) with the same value, lets say 0.5, such that the output looks like this:
Columns 1 through 6
1.0894 1.1923 1.0238 1.0625 1.0222 0.9701
1.0000 1.2419 1.2093 0.9412 0.9783 1.0923
1.0379 **0.5** 1.0000 1.0417 1.0667 1.0000
Columns 7 through 12
0.9661 0.9568 1.0400 0.8519 0.8675 0.9821
0.7895 1.0000 NaN 1.0000 1.1250 0.8182
1.0667 0.7714 **0.5** 1.0000 0.9383 1.1111
I know I can replace all NaN in my matrix by 0.5 using this:
data2winner(isnan(data2winner)) = 0.5
So i thought using
data2winner(isnan(data2winner(3,:))) = 0.5
would solve my problem, but it does not work. Does anybody have an idea how to solve my problem? Any help would be greatly appreciated!
Upvotes: 2
Views: 841
Reputation: 221504
isnan(data2winner(3,:))
gives you a logical array of ones and zeros where NaNs
are present or not respectively in the third row. Then, you were needed to index into the third row of the input matrix and select the columns that had NaNs
(ones from isnan
output) and set them to 0.5
. Your mistake was indexing into the complete matrix instead of only the third row.
Thus, this should do it -
row_id = 3;
data2winner(row_id,isnan(data2winner(row_id,:))) = 0.5
Upvotes: 3