"What is the matter with you?" - Is it right?

How do we change the sentence “He asked,” What is the matter with you?"" from direct speech into indirect speech?Is it"He asked what was the matter with me."?
Is “He asked what the matter was with me.” right?

Both are correct. But I think the second is more preferable. (word order in indirect questions or I wonder-questions)
Anyway, ask other people, because I’m not a native speaker.

If “you” reports that sentence, then I think the most likely way to begin a reported speech sentence using ‘asked’ would be “He asked me…”.

  1. He asked me what was the matter.

You could also begin the reported sentence with “He wanted to know…”

  1. He wanted to know what was the matter with me.

Neither of the sentences above follows the most common word order for reported questions, however the word order sometimes remains interrogative when the verb ‘be’ is the main verb in the question.

You could change sentences 1 and 2 to these:
3. He asked me what the matter was.
4. He wanted to know what the matter with me was.
However, I don’t think sentences 3 and 4 sound quite as natural as sentences 1 and 2.
.

I wouldn’t use “asked” there. I’d use “said”. He said," What is the matter with you?"

Then you can report “He asked what was the matter with me”. You are only reporting his words, not yours.