You are on the right track. Let's start with:
Indexing into x1 and x4 is your problem here. Think about the difference between how you index into a 2D matrix (x1) and a 1D vector (x4).
Once you work that out, then rethink how to do this for-loop properly:
for i = 1:1:length(x1) ...
Lastly, you need to think about yout "X". The way you have set it up, the value in X is overwritten every iteration of the for-loop. So you will only get the last value of X out from your loop. So you'll need to think about how you can get every calculated "X" out from your for-loop.
Finally, not sure of the exact problem that you were asked to solve. I suspect that you may be misreading it. I have a suspision that your original problem may related to the function quantile().