|
|
Finding n largest values in a matrix
I've been programming in Matlab for quite a while now, but recently I ran into a problem that I can't solve in a neat and efficient way. I have a 2D matrix A (lets say 32x32) and a vector B (lets say its length is 16). Now I want to find 16 largest values in matrix A. Then I want to add first component of vector B to largest element in A, second component of B to second largest element in A and so on. Can anyone give me a hint how can I do it without using time consuming iterative algorithm?
|
|
0
|
|
|
|
Reply
|
Jan
|
1/11/2010 11:24:03 AM |
|
Are you sure those are really the sizes of your matrices? Because I
don't really consider nanoseconds "time consuming." Now if they were
thousands of elements in each direction, then it could be time
consuming. Anyway, did you look at sort()?
|
|
0
|
|
|
|
Reply
|
ImageAnalyst
|
1/11/2010 11:39:16 AM
|
|
Dear Jan!
> I've been programming in Matlab for quite a while now, but recently I ran into a problem that I can't solve in a neat and efficient way. I have a 2D matrix A (lets say 32x32) and a vector B (lets say its length is 16). Now I want to find 16 largest values in matrix A. Then I want to add first component of vector B to largest element in A, second component of B to second largest element in A and so on. Can anyone give me a hint how can I do it without using time consuming iterative algorithm?
Have you seen Bruno's MinMax selection function?
http://www.mathworks.com/matlabcentral/fileexchange/23576
Usually this is much faster than SORT.
Good luck, Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
1/11/2010 12:03:05 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
Jan
|
1/11/2010 12:12:02 PM
|
|
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.
|
|
0
|
|
|
|
Reply
|
Jan
|
1/11/2010 12:13:03 PM
|
|
"Jan " <fremenzone@poczta.onet.pl> wrote in message <hif4ii$rur$1@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
|
|
0
|
|
|
|
Reply
|
Jos
|
1/11/2010 12:59:05 PM
|
|
Works like a charm! Thanks very much.
I didn't know that I can convert 2D matrix to a vector using A(:).
|
|
0
|
|
|
|
Reply
|
Jan
|
1/11/2010 1:57:04 PM
|
|
|
6 Replies
855 Views
(page loaded in 0.103 seconds)
|
|
|
|
|
|
|
|
|