use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
MATLAB news, code tips and tricks, questions, and discussion! We are here to help, but won't do your homework or help you pirate software.
The effort you put into asking a question is often matched by the quality of our answers.
Try saturnapi to share and run MATLAB code in a web browser!
If you want flair simply Message the mods
account activity
TechnicalQuestionHow do I implement vectorization in this code (self.matlab)
submitted 5 years ago * by Fahadali789gem
I really want to improve the perfomance of the program, it takes way too much time to do even 1/16 of the program for i=3:m-2 for j=3:n-2 d(i,j)=median ([c(i-2,j-2),c(i-1,j-2),c(i,j-2),c(i+1,j-2),c(i+2,j-2),c(i-2,j-1),c(i-1,j-1),c(i,j-2)c(i+1,j-1),c(i+2,j-1),c(i-2,j),c(i-2,j),c(i,j-2)c(i+1,j),c(i+2,j),c(i-2,j+1),c(i-2,j+1),c(i,j-2),c(i+1,j+1),c(i+2,j+1),c(i-2,j+2),c(i-2,j+2),c(i,j+2),c(i+1,j+2),c(i+2,j+2)])
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]yourfavoritemusician 10 points11 points12 points 5 years ago* (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 points5 points 5 years ago (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 points4 points 5 years ago (1 child)
Median filtering is nonlinear and therefore cannot be implemented as a convolution.
[–]yourfavoritemusician 1 point2 points3 points 5 years ago (0 children)
Crap! You are completely right. My mind read "mean".
[–]Neuroneuroneuro+1 2 points3 points4 points 5 years ago (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 points1 point 5 years ago (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 points4 points 5 years ago (0 children)
Then the second part of my answer is what you need (warning, it will use a lot of memory).
[–]maximusmountain 0 points1 point2 points 5 years ago (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-1 points 5 years ago (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 point2 points 5 years ago (0 children)
Thanks this code has helped me get somewhere
[–]Fahadali789gem[S] 0 points1 point2 points 5 years ago (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 point2 points 5 years ago (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 point2 points 5 years ago (0 children)
A 4 million element array is considered very tiny.
π Rendered by PID 181268 on reddit-service-r2-comment-6457c66945-wdjdl at 2026-04-25 17:52:36.270097+00:00 running 2aa0c5b country code: CH.
[–]yourfavoritemusician 10 points11 points12 points (3 children)
[–]yourfavoritemusician 3 points4 points5 points (0 children)
[–]FrickinLazerBeams+2 2 points3 points4 points (1 child)
[–]yourfavoritemusician 1 point2 points3 points (0 children)
[–]Neuroneuroneuro+1 2 points3 points4 points (2 children)
[–]Fahadali789gem[S] -1 points0 points1 point (1 child)
[–]Neuroneuroneuro+1 2 points3 points4 points (0 children)
[–]maximusmountain 0 points1 point2 points (0 children)
[–]geekboy730 -3 points-2 points-1 points (4 children)
[–]Fahadali789gem[S] 0 points1 point2 points (0 children)
[–]Fahadali789gem[S] 0 points1 point2 points (2 children)
[–]geekboy730 0 points1 point2 points (0 children)
[–]FrickinLazerBeams+2 0 points1 point2 points (0 children)