MATLAB: Finding element with max value in each row of a matrix

element positionrow max

How can I find the 4 elements with the largest values in each row of a matrix ? Moreover, I would like to know the exact position of them in each row.
i.e. A(2X8) = [ 1 2 3 4 8 5 2 9 ; 5 6 4 7 9 1 1 2; ]
For the above matrix the answer would be:
1st row max values elements: 4, 8, 5, 9
1st row position of these elements: 4, 5, 6, 8
2nd row max values elements: 5, 6, 7, 9
2nd row position of these elements: 1, 2, 4, 5
Any help could be useful.
Thanks in advance!

Best Answer

a=[ 1 2 3 4 8 5 2 9 ; 5 6 4 7 9 1 1 2; ]
[ii,jj]=sort(a,2,'descend')
v=ii(:,1:4)
idx=jj(:,1:4)