What is the best way to mark some elements in MATLAB? using NaN or Inf? or something different?

As you may know, in many cases it is necessary to note some elements of the matrix. For example, when we have a weighted adjacency matrix, and our graph is not completely connected, we need to note some elements to show that there is no edge between these nodes. The question is how to do this? Is it better to put NaN or Inf on these elements in the matrix? or something else (e.g. -1)?

+5
source share
1 answer

It completely depends on the case. In the example you gave a good solution, you could use zeros , because the edges are weighted, and for many purposes, the edge of weight 0 is equivalent to no edge. This is true if you do things like thread / cut algorithms.

Usually, choosing between NaN and Inf, I would go with NaN . Inf has some properties that you might not like as an “invalid” marker:

Inf*(-1) = -Inf
Inf+(-Inf) = NaN
Inf > 10 = True
etc...

A pure solution might be to support another logical matrix that has True, where the connection is valid, and False otherwise. It takes up a little memory, but if your matrix is ​​not huge, I think that the readability of the code that it gives you is worth it.

+10

All Articles