Peter Atilla, 46, from Northampton, said he had a sleep condition called sexsomnia.
Mr Atilla was accused of having sex with a woman while she slept after she invited him in bed "for a cuddle".
Methyr Tydfil Crown Court heard he had returned from a tour of Afghanistan the day before, and may have been in "action mode" when he went to sleep.
Two psychiatrists told the court it was "possible" the defendant suffered from parasomnia - defined as unwanted behaviour during sleep.
Dr Chris Idzikowski said a subset of parasomnia was sexsomnia - a type of "confusional arousal" - which can lead to sexual behaviour from a person in deep sleep.
Dr Idzikowski said: "If he arrived back in action mode it's potentially something that would come through in his sleep.
He said Mr Atilla, a former Royal Logistics Corps officer who served "with distinction" in the first Gulf War, Kosovo, Iraq and Afghanistan, may have been triggered by something as simple as a sound.
He admitted the evidence the disorder led to Mr Atilla having sex with the woman while asleep was "weak" but said he could not exclude it from being possible.
Another psychiatrist, Dr Chandan Seghal, said he too "could not exclude" the attack relating to a sleep disorder, but agreed the evidence was "weak."
Mr Atilla told the court he had "no memory" of the alleged incident.
A former soldier accused of rape has been cleared after a court heard he was suffering from a sleep disorder.