A federal judge here yesterday ordered the Air Force to reinstate and give full back pay to Leonard P. Matlovich, a former Air Force sergeant who was kicked out of the service five years ago after he openly declared his homosexuality.

U.S. District Court Judge Gerhard A. Gesell said Matlovich's dismissal was unlawful because the Air Force had engaged in "perverse behavior" in failing to explain clearly its policies on when it retains homosexuals in the service and when it boots them out.

The judge said that Matlovich, 37, a Bronze Star Vietnam war veteran who has been working in a San Francisco warehouse since his dismissal, should be "promptly reinstated" at the rank and salary he would have obtained had he not been "unlawfully discharged." It was not immediately clear whether matlovich, a technical sergeant working as a human relations counselor, will be promoted when he is reinstated.

Government attorneys declined to estimate how much Matlovich is owed in back pay, but the airman's attorneys said they expect the amount is in excess of $50,000, minus any money that he has earned as a civilian since 1975. e

Matlovich began what became a landmark civil rights case when he challenged Air Force regulations that prohibit the retention of homosexual servicemen unless "the most unusual circumstances exist." Despite the fact that Matlovich won several medals, was wounded in Vietnam and had received numerous laudatory performance reports for his work, the Air Force said "meritorious conduct is not enough" to retain a homosexual serviceman.

Gesell, in a sharp attack on the Air Force, said the service had "not only misrepresented the standards applied [to homosexuals] in the past, but the Air Force is totally unable to clarify its position generally, or to Matovich."

Matlovich jumped up and embraced his attorneys after the hearing, saying his "five-year battle" was over.

The government, which has 60 days to appeal the ruling, refused to comment on the decision or say whether it would appeal. Justice Department lawyer Thomas R. Kline argued during the hearing that the Air Force had, in fact, clarified its position on homosexuals by stating that they are only retained in the the services under unusual circumstances.

In July 1976, Gesell originally upheld the Air Force's ouster of Matlovich, but urged the service to reexamine its antihomosexual policies that led to his dismissal.

Matlovich appealed his case to the U.S. Court of Appeals, which ruled in 1978 that the Air Force had not properly defined the various exceptions under which a homosexual serviceman could remain in the service, and why Matlovich did not qualify. The appellate court ordered the Air Force to come up with an explanation of its policy and sent the case back to Gesell.

In the past year, however, the Air Force filed several contradictory statements regarding its policy toward homosexuals in the service. As a result, Matlovich's attorneys charged the government had "trifled' with the court order requiring it to define the conditions under which homosexuals may serve in the Air Force.

"Since you keep changing your mind every time you come before me," Gesell told Kline at one point, "they [Matlovich's lawers] want to find out which of your positions are true."

Gesell said that he had "repeatedly, insistently, repetitiously, pointedly" asked the Air Force to review its files and produce the standards for which it made decisions on retaining gays in the service.

Now that the court of appeals has made the same request, Gesell said, "as a practical matter, I don't think that [the Air Force] complied with that order at all. I don't see any sign of it.'

Kline argued that the Air Force had shown a rational basis for its dismissal of Matlovich. "It has been the long-held opinion of the Air Force that homosexuality . . . impacts on discipline and morale," he said. "The exception is extremely narrow . . . since it has never been exercised, it is impossible to say when it will be."

The Air Force had originally argued that its regulations contained "an exception provision" that was potentially available to Matlovich. Later, Secretary of the Air Force Hans M. Mark stated the Air Force "does not recognize exceptions to the policy of discharging confirmed, practicing homosexuals." Then last week Mark filed additional papers saying his most recent statements were "incorrect."

Matlovich's attorney, Patricia D. Douglass, argued that the case "illustrated precisely why agencies that affect people's lives have to have standards" so they "cannot keep shifting ground."

Matlovich, who had flown from San Francisco for the court hearing, sat nervously, with his fingers crossed, awaiting Gesell's ruling after the legal arguments.As Gesell read his order, Matlovich smiled to friends sitting in the crowded courtroom. "Wow," he whispered to his attorneys at one point.

Matlovich said later that he fully intends to serve again in the Air Force and that he hopes the case would be a message to the military to change its policies regarding homosexuals.

"It's been a very, very hard battle," he said, as one well-wisher approached him and addressed him as "sgt. Matlovich."