Welcome to e health forum.
Since you are able to have erections early in the morning and when you stimulate your genitals (masturbate), it is more likely that you do not have any physical conditions affecting your sexual function.
Some tips for your help include:-
1. Cut down on smoking, alcohol, and illegal drugs.
2. losing excess weight,
3. Get plenty of rest and take time to relax.
4. Exercise and eat a healthy diet to maintain good circulation.
In case you are single, then it is more likely that your issues may be more of a psychological. In case of psychogenic issues, reducing your stress and feelings of guilt would be the first step. Realize that you cannot force yourself to have a sexual response, just as you cannot force yourself to go to sleep or to perspire. The harder you try to have a certain sexual response, the harder it becomes to respond. To reduce the pressure, absorb yourself in the pleasure of the moment.
Masturbation is not the cause of your issues. In fact masturbation among singles is a safe alternative to sex, which helps to relieve the sexual tension and frustration. Masturbation done occasionally does not result in sexual dysfunction. But over indulgence (frequent masturbation throughout the day) can be a factor in difficulty to orgasm during normal sexual intercourse.
In case you still have doubts, then consulting a doctor (sexologist) for a examination and specific advice might be helpful.