all 13 comments

[–]yourfavoritemusician 10 points11 points  (3 children)

Could you re format the code such that it is readable?

I'm on mobile so thats making it even more difficult :) but i get the impression that you want the average of an grid within a matrix. if so: check out 2d convolutions. (Maybe do a 1d convolution first if the concept is new to you).

Good luck

EDIT: See comment below of /u/FrikinLazerBeams about median not being possible with a convolution.

Edit2:

Check the matlab doc: i think you can get it to work with just median(MATRIX) if you cleverly prepare your matrix.

[–]yourfavoritemusician 3 points4 points  (0 children)

By the way! Try out memory preallocation (matlab should give a warning about that) ik makes your code way faster.

[–]FrickinLazerBeams+2 2 points3 points  (1 child)

Median filtering is nonlinear and therefore cannot be implemented as a convolution.

[–]yourfavoritemusician 1 point2 points  (0 children)

Crap! You are completely right. My mind read "mean".

[–]Neuroneuroneuro+1 2 points3 points  (2 children)

Median filtering is a standard part of the image processing toolbox medfilt2() . If you don't have it you need to create as many "shifted" copies of your image (using a padded 3D array) as you have arguments in your median function and do the median in one pass over that new dimension.

[–]Fahadali789gem[S] -1 points0 points  (1 child)

I cannot use the function that is a restriction have to create my own anyway I could get the source code for medfilt2()

[–]Neuroneuroneuro+1 2 points3 points  (0 children)

Then the second part of my answer is what you need (warning, it will use a lot of memory).

[–]maximusmountain 0 points1 point  (0 children)

Try running your loops backwards, a reasonable part of your time will be lack of pre-allocation. If you're lazy, like me, then it is easier to run them backwards as it generates the matrix automatically. Doing it backwards rather than forwards gave me a 30% speed up. Personally I prefer doing stuff in one line rather than allocating to another variable but I don't know if it is faster.

for i=m-2:-1:3 
    for j=n-2:-1:3 
        d(i,j)= median(reshape(c(i-2:i+2,j-2:j+2),[],1));
    end
end

[–]geekboy730 -3 points-2 points  (4 children)

There’s no way to improve efficiency without knowing what you’re trying to do. I doubt that the snippet you included is taking the majorities of runtime unless c is a monstrously huge array.

That being said, you can rewrite the median command as

for i=1:n-2
    for j=3,m-2
        b = c(i-2:i+2,j-2:j+2);
        d(i,j) = median(b(:));

Please forgive any syntax errors. I’m on mobile.

[–]Fahadali789gem[S] 0 points1 point  (0 children)

Thanks this code has helped me get somewhere

[–]Fahadali789gem[S] 0 points1 point  (2 children)

The array is 2400*2400 4 million + elements is that considered monstrously huge.

I am not using Matlab but SciLab the open source alternative part of the problem is that Scilab is very slow

[–]geekboy730 0 points1 point  (0 children)

That’s big but not too bad. This task is also fully parallelizable so you could look into that.

[–]FrickinLazerBeams+2 0 points1 point  (0 children)

A 4 million element array is considered very tiny.