COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Finding n largest values in a matrix

• Email
• Follow

```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

See related articles to this posting

```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

```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

```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

```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

```"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.

% 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

```Works like a charm! Thanks very much.
I didn't know that I can convert 2D matrix to a vector using A(:).
```
 0

6 Replies
1205 Views

Similar Articles

11/30/2013 5:28:51 AM
page loaded in 63024 ms. (0)