How can I improve the performance of my MATLAB code when using loops?

  • MATLAB
  • Thread starter UrIkOn
  • Start date
In summary, the conversation is about using looping to implement a formula in MATLAB, but it causes the program to hang. The speaker suggests that the problem may be due to the long computation time of the innermost statement in the code. They also mention that using FFTs may be a faster alternative. The conversation then shifts to discussing a school assignment on image processing, where the speaker is stuck on a particular part of the assignment and asks for clarification. The expert suggests using vectorization and reducing the number of calls to a complex exponential to improve computation speed. The conversation ends with the expert summarizing the assignment's steps and providing suggestions for improving performance.
  • #1
UrIkOn
5
0
I use looping to implement this formula
https://www.physicsforums.com/attachment.php?
attachmentid=46840&stc=1&d=1335864568
But,when I run, mathlab will hang?Why?
Code:
%Import picture into Matlab
img=imread('picture.jpg','jpg');
img512=imresize(img, [512 512]);
imgGRAY=uint8(rgb2gray(img512));
figure(1);imshow(img);
figure(2);imshow(imgGRAY);

M=512;
N=512;
f=double(imgGRAY);
F=zeros(512);
for u=1:512
    for v=1:512
        TotalN=0;
        for x=1:512
            for y=1:512
                TotalN=TotalN+(f(x,y)*exp(-1i*2*pi*(((u*x)/512)+((v*y)/512))));
            end
        end
        F(u,v)=(1/(M*N))*TotalN;
    end
end
 

Attachments

  • picture.jpg
    picture.jpg
    50.8 KB · Views: 426
  • Capture.JPG
    Capture.JPG
    3.3 KB · Views: 419
Last edited:
Physics news on Phys.org
  • #2
UrIkOn said:
But,when I run, mathlab will hang?Why?
That innermost statement, TotalN=TotalN+(f(x,y)*exp(-1i*2*pi*(((u*x)/512)+((v*y)/512)))); is being performed 5124 or 68,719,476,736 times! Even if you coded this in Fortran or C it would take a long time. Matlab is a couple of orders of magnitude slower than either of those languages. If I did this calculation in python on my computer it would take over a day to complete. Matlab is perhaps a bit faster than python, but not much.

Try seeing how long your convolution takes with a 64x64 image. Multiply by 4096. That's how long your convolution will take with a 512x512 image.
 
  • #3
Then is there any solution?
 
  • #4
UrIkOn said:
Then is there any solution?
Either wait a long time or do something else. What motivated this particular kernel?
 
  • #5
I don't understand, what you mean for?
 
  • #6
UrIkOn said:
I don't understand, what you mean for?
Why are you using this kernel?
 
  • #7
School Assignment:image processing
 
  • #8
Tell us the assignment. Perhaps you misread it. If not, you might be stuck waiting for a day or so to see the results.

Have you learned about FFTs?
 
  • #9
Ya,I learned fft before.
This is the assignment question:
1. Take a photo with your own camera and import it into Matlab converting it to a 8-bit512x512
pixel gray scale imageI(x,y).
2. Display the original and the gray-scale image.
3. Write your own function to implement the Discrete Fourier Transform (DFT) in Matlab (do NOT use the Matlab function for FFT) of I:
4. Write the Matlab code to implement the functionf(x,y) for the inverse DFT of a 2D array of complex numberF(u,v):
5. Visualise the DFT of your grayscale imageIusingimshow().
(Hint: You may need to scale using log to display the information within the range of the image pixel values, and center so that the origin is shifted to the centre of a 512x512.)
6. Construct a 512x512 template for alow pass lter using a Cosine curve, where thehigh frequency components are zeroed. Apply this lter to the DFT of your image. Take the inverse DFT and visualise the Real, Imaginary and Phase. Compare with your original image. Describe each of the 5 visualised outputs (Original image, Filtered image, DFT Real, DFT Imaginary and DFT Phase).
7. Construct a 512x512 template for ahigh pass lterusing a Sine curve where thelow frequency components are zeroed. Apply this lter to the DFT of your image. Take the inverse DFT and visualise the Real, Imaginary and Phase. Describe each of the 5 visualised outputs (Original image, Filtered image, DFT Real, DFT Imaginary and DFT Phase).
 
  • #10
The chief culprit here is that you are computing the complex exponential 5124 = 68,719,476,736 times. The complex exponential obeys the identity [itex]\exp(u+v) = \exp(u)\cdot \exp(v)[/itex]. Couple this identity with the fact that your image is square and you can reduce the number of calls to the complex exponential to 5122 = 262,144.
 
  • #11
Also, MATLAB is horrible with loops. If you can vectorize your problem it will compute much faster.
 

1. Why does my MathLab keep freezing or hanging?

There could be several reasons for this issue. One possibility is that your computer's hardware may not meet the minimum system requirements to run MathLab smoothly. Another reason could be outdated software or drivers. It is also possible that there is a conflict with other programs running on your computer.

2. How can I prevent my MathLab from hanging?

To prevent MathLab from freezing or hanging, make sure your computer meets the minimum system requirements. It is also important to regularly update your software and drivers. Additionally, try closing any unnecessary programs while running MathLab to avoid conflicts.

3. Does having too many tabs open in MathLab cause it to hang?

Yes, having too many tabs open in MathLab can cause it to hang. It is recommended to close any unnecessary tabs or windows to improve the performance of MathLab.

4. Is there a way to fix my MathLab when it hangs?

If your MathLab freezes or hangs, you can try restarting the program or your computer. It is also helpful to clear your browser's cache and cookies. If the issue persists, you may need to contact your IT department or MathLab support for further assistance.

5. How can I improve the performance of my MathLab?

To improve the performance of MathLab, make sure your computer meets the minimum system requirements. You can also try closing any unnecessary programs or tabs while running MathLab. Additionally, regularly updating your software and drivers can help improve performance.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
Back
Top