You can use the isnan
function to create a logical matrix of the same size as your matrix. The isnan
function returns a matrix of 1's where the input is NaN and 0's everywhere else. You can then use this logical matrix to set the elements of your matrix that are NaN to zero.
Here's an example:
main.m79 chars6 lines
In this example, we create a 3x3 matrix A
with some NaN values. We then create a logical matrix nan_logical
that is true where A
is NaN. Finally, we use the logical matrix to set the NaN values to 0 in A
.
You can also accomplish this in a single line by combining the isnan
and indexing operations:
main.m17 chars2 lines
This will have the same effect as the previous example.
gistlibby LogSnag