From: Jos (10584) on
"Jan " <fremenzone(a)poczta.onet.pl> wrote in message <hif4ii$rur$1(a)fred.mathworks.com>...
> Size of matrices is just an example - they will be bigger (1024x1024) and this operation will be run thousands of times. Even if it weren't I still want to create my code that is as fast as possible :)
>
> I'm afraid sort() (or sortrows() ) won't solve my problem. At least I don't see how I could use these functions. sort()/sortrows() can give me indices of elements in a column/row, but it's possible that 2-nd largest element in column/row 1 is greater than largest element in column/row 2. I need something that gives global results.

What about this

% data
n = 4 ; A = magic(n) ; % a large matrix of size n-by-n
m = 4 ; % replace m highest values with elements of B
B = -(1:m) ;

%engine
[si,si] = sort(A(:),'descend') ;
A(si(1:m)) = B

hth
Jos
From: Jan on
Works like a charm! Thanks very much.
I didn't know that I can convert 2D matrix to a vector using A(:).