Madrid should be fine, as long as you're sure it's the same plane. BA use T4S, Iberia use T4, so make sure you don't actually have to do the dash between the too! (If you do, you'll need to clear immigration, head down several floors, catch the train, go up, go out to the end of the terminal, exit through immigration)
Assuming a BA back-to-back at Madrid T4S, you come straight out into the terminal at the gate, and then grab a seat and wait for boarding. Or, it's a 4-5 minute walk to the lounge if there's time!