13

5

I want to compile the following function which takes a matrix variable.

```
ClusterFind =
Compile[{{Inte, _Real}, {Matrix}},
Map[If[Last@# == Inte, Take[#, 4]] &, Matrix], CompilationTarget -> "C",
RuntimeAttributes -> {Listable}, Parallelization -> True];
```

Here `Matrix`

is a $m\times n$ matrix of real,integer or complex numbers. I also would like to know if it is possible to compile without knowing the dimension of the variable matrix beforehand. Can the `Matrix`

in the above example be of `PackedArray`

data type?

**UPDATE**

Thanks for the suggestions. I found the following compiled version most efficient as far as speed is concerned.

```
ClusterFind =
Compile[{{Inte, _Real}, {Matrix, _Complex, 2}},
Take[Select[Matrix, (Last@# == Inte) &], All, 4],
CompilationTarget -> "C"];
```

BR

On my machine, @Oleksandr's version is faster than the one you added in your edit. Is that not so on yours? – acl – 2012-02-03T17:43:39.977

@acl I gave sixteen runs for clustering with sixteen integers from 2 to 16 and I took a complex matrix of dimension {750000, 5}. I tried with my function and that of Oleksandr's. Mine took 3.54 sec. and his took 4.98 sec. Funny that u get opposite results. – PlatoManiac – 2012-02-03T19:09:46.563

Sorry! I meant 15 runs.. – PlatoManiac – 2012-02-03T19:16:14.557

Sorry; I didn't understand from the wording you used in the original question that the matrix had only five columns. I had assumed it would have more, so I extracted the five needed columns into a temporary matrix to avoid copying the larger matrix. As it turns out this step was unnecessary, so yes, your version without the extra operation should indeed be faster. (@acl, if you found mine faster, I guess you also assumed the matrices had more than 5 columns?) – Oleksandr R. – 2012-02-06T23:04:25.977

@OleksandrR yes i tried for randomly chosen sizes – acl – 2012-02-06T23:19:02.417