MATLAB: Sorting and Equating n-number of least Values in the row to NaN

indexsort

How does one equate say 5 least values in a row of matrix to NaN and have them reflected in the original matrix and not the sorted one. For example;
A = rand(10,10)
[srt, idx] = sort(A(10,:),'ascend');
srt(1:5)=NaN;
idx(1:5)=NaN;
A(10,:) = srt
This works fine but the NaN values only apply in the sorted A. I want them to be reflected according to the index in the original Matrix

Best Answer

[~, idx] = sort(A(10,:),'ascend'); % keep the index; don't care about the values really
A(10,idx(1:5))=nan; % stuff NaN in the first five locations (minima)
idx(1:5)=NaN;
in your original just clobbered the indices of the elements you wanted.