We study the behavior of the inverse participation ratio and the localization transition in infinitely large random matrices through the cavity method. Results are shown for two ensembles of random matrices: Laplacian matrices on sparse random graphs and fully connected Lévy matrices. We derive a critical line separating localized from extended states in the case of Lévy matrices. Comparison between theoretical results and diagonalization of finite random matrices is shown.