Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:11:48+00:00 2026-06-11T14:11:48+00:00

I started learning a little Matlab a couple days ago. I wanted to plot

  • 0

I started learning a little Matlab a couple days ago.

I wanted to plot a Dirac-comb-like approximation, so given that I knew some functional programming, and that I have been told “you shouldn’t need for loops in Matlab”, I ended up with this:

M = 50
dx = 0.1
r = 20
x = -r/dx:r/dx
y = arrayfun(@(k) dx .* sum(exp(-2j * pi * dx * k * (-M:dx:M))), x)

But I feel arrayfun isn’t a good way to do this — it just feels awkward/overkill in Matlab.

Or maybe it’s just me, I don’t know.
Is there a better way to plotting this graph without resorting to arrayfun, or is this the best way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:11:50+00:00Added an answer on June 11, 2026 at 2:11 pm

    First, use semicolons behind commands to suppress output, it really makes a difference in performance:

    M  = 50;
    dx = 0.1;
    r  = 20;
    x  = -r/dx : r/dx;
    

    Then, dot-operators (.*, ./, etc.) are for element-wise operations. The multiplication you do inside the arrayfun (dx .* sum(exp(...))) is a scalar times a vector. In this case element-wise and normal multiplications are the same. It is a good habit to keep normal multiplications for scalar*vector; it helps prevent bugs.

    Then, the arrayfun is unneccesary. You can accomplish the same like so:

    y = dx * sum( exp(-2j*pi*dx * (-M:dx:M).'*x) );
    

    The product -2j*pi*dx is a product between all scalars. The product (-M:dx:M).'*x however is a product between matrices. Since sum sums down the columns (dimension 1) by default, the results are the same. This solution has larger memory overhead, but is a lot faster than arrayfun.

    Note that I’ve used .' for transpose. In Matlab, the notation A' means conjugate transpose, and A.' means normal transpose. Especially in the context of complex math like you have, this is very important. Learn the difference, and remember it well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I have started learning rails and was a little surprised that the default
Hey, I've just started learning JavaScript and I'm making a little script that generates
I've recently started learning Python and wrote a little script that informs me when
I've just started learning Objective-C and made a little compass app that will display
I started learning Rails only a few days ago, but in the mean time
I've started learning (and using) Monorail a little while ago, and recently, I've dabbled
Started learning Wicket after ASP.NET MVC and feel a little bit confused about managing
Started learning algorithms. I understand how to find theta-notation from a 'regular recurrence' like
I started learning JavaScript a while ago. It's a fairly easy programming language considering
I've just started learning Android development and as a little project, I'm building a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.