Hi,

I am manipulating an matrix (one or more lines) that has the priority numbers of another one, for example:

```
priority = [0.1 0.9 0.4 0.7 0.3] # matrix filled using rand()
A = [col1 col2 col3 col4 col5] # matrix filled with rand(0:1)
```

So the order of the matrix A that I want is:

```
[col1, col5, col3, col4, col2]
```

Or at least I want the order of the columns with their value equal to 1.

One way to proceed with this is to use the line below

```
order = sortperm(view(priority, h, :))
```

But I need this code to run as fast as possible. Is there any way to do it?

If you find yourself repeatedly sorting arrays (which presumably is why it is performance critical), there is a good chance you should use a different data structure.

7 Likes

It looks like you’re sorting a row view, this will likely be less efficient than sorting a column view due to the memory layout.

3 Likes

@stevengj Which kinds do you suggest?

@baggepinnen Thanks, I’ll change that.

Depends on what you are doing. What kinds of operations do you need on the data? Why are you sorting repeatedly?

1 Like

Because the vector `priority`

changes its value at great frequency, so every time I run the function, I need to see its value.

I’m creating a heuristic procedure to generate solutions to a floor shop problem, and this procedure needs to be very randomized to generate good solutions, and the representation of the solution I got from the literature, so I think it needed to be the way I’m doing, sorting in a certain frequence.

what about a heap based priority queue?

1 Like

Are you using `rand`

+ `sortperm!`

instead of `shuffle!`

then?

3 Likes

Good question. If the priorities are random anyway, you’re much better off using `shuffle!`

. It will be faster and very slightly more statistically correct.

1 Like

Thanks you all. I used another representation for the solution to avoid sortperm.