One of the most serious causes of disease in the world's population, which kills many people worldwide every year, is heart attack. Various factors are involved in this matter, such as high blood pressure, high cholesterol, abnormal pulse rate, diabetes, etc. Various methods have been proposed in this field, but in this article, by using sparse codes in the classification process, higher accuracy has been achieved in predicting heart attacks. The proposed method consists of two parts: preprocessing and sparse code processing. The proposed method is resistant to noise and data scattering because it uses a sparse representation for this purpose. The spars allow the signal to be displayed at its lowest value, which leads to improve computing speed and reduce storage requirements. To evaluate the proposed method, the Cleveland database has been used, which includes 303 samples and each sample has 76 features. Only 13 features are used in the proposed method. FISTA, AMP, DALM and PALM classifiers have been used for the classification process. The accuracy of the proposed method, especially with the PALM classifier, is the highest among other classifiers with 96.23%, and the other classifiers are 95.08%, 94.11% and 94.52% for DALM, AMP, FISTA, respectively.