0

I've been having a go at speeding up the following code snippet, but I've got no idea where to start.

I have a function denoise(i, j) that takes a matrix index i, j as an input and returns an element p. The function denoise is able to reference the matrix to which the index refers.

The intention is to input an image matrix M, and return a target image T where T(i,j) = denoise(i,j) for all i,j in M

My code at the moment is very slow because it uses for loops. What's the best place to start to speed it up?

target = zeros(rows_in_M, cols_in_M)
for i=1:rows_in_M
    for j=1:cols_in_M
        target(i, j) = denoise(i,j)
    end
end
Alejandro
  • 7,290
  • 4
  • 34
  • 59
adp
  • 1
  • 2
    Does `donoise` accept a vector or matrix as input? – Aziz Dec 04 '17 at 17:53
  • Because of matlab's underlying VM and just in time compiler, `for` loops are not as slow as you would assume. I'm looking up a post that has some benchmarks. – Alea Kootz Dec 04 '17 at 23:19
  • Here's a fairly detailed post about various arraignments and their benchmarked times. IIRC, The greatest difference to be had is to keep matrixes either collumwise, or put the internals on denoise inside the for loops and not call it as a separate function. https://stackoverflow.com/questions/12522888/arrayfun-can-be-significantly-slower-than-an-explicit-loop-in-matlab-why?noredirect=1&lq=1 – Alea Kootz Dec 04 '17 at 23:25
  • Aziz, `denoise` takes in two integers, `i, j`, that are matrix indices. So `T(1,1)` would be the de-noised pixel at `M(1,1)` – adp Dec 05 '17 at 13:13

0 Answers0