I am a fan of booking an apartment when in a city for more than a few nights. And when in apartments, I prefer to be in residential neighborhoods near, but not smack in the middle of tourist areas. I shop for groceries with locals, I have coffee in the neighborhood place. Not only is this often a good value, but also lets me experience a place in a more integral way, a way that's harder to achieve when in a hotel.
In Madrid, I'd look at places outside of the triangle drawn by connecting Gran Via, Sivilla Metro, the plazas Sol & Mayor, and the Royal Palace. Just north, south, or east of there is the sweet spot.