# MATLAB: Guys!! help me in code for finding out maximum value in each row of a matrix and make others as zero value in such a way that if i need 2 max in a row it should display without changing the index value and make others as zero

find functionwith sort

Eg: a=[1 2 3; 4 5 6; 7 8 9] if i need R=2 means it should display like rowmax=[0 2 3; 0 5 6; 0 8 9]

>> R = 2;>> A = [1,2,3;4,5,6;7,8,9];>> [~,B] = sort(A,2);>> [~,B] = sort(B,2);>> X = B>size(A,2)-R;
>> C = zeros(size(A));>> C(X) = A(X)C =   0     2     3   0     5     6   0     8     9
>> A(~X) = 0A =   0     2     3   0     5     6   0     8     9
>> A = [randperm(5);randperm(5);randperm(5)]A =   5     4     2     3     1   4     3     2     5     1   5     2     4     3     1>> [~,B] = sort(A,2);>> [~,B] = sort(B,2);>> X = B>size(A,2)-R;>> A(~X) = 0A =   5     4     0     0     0   4     0     0     5     0   5     0     4     0     0