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 667177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:53:58+00:00 2026-05-13T23:53:58+00:00

I came into matrix world from loop world (C, etc) I would like to

  • 0

I came into matrix world from loop world (C, etc)

I would like to call a function on each individual member of a vector/matrix, and return the resulting vector/matrix.

This is how I currently do it:

function retval = gauss(v, a, b, c)
  for i = 1:length(v)
    retval(i) = a*(e^(-(v(i)-b)*(v(i)-b)/(2*c*c)));
  endfor
endfunction

Example usage:

octave:47> d=[1:1000];
octave:48> mycurve=gauss(d, 1, 500, 100);

Now, all advice on MATLAB/Octave says: STOP whenever you catch yourself using loops and think of a better way of doing it.

Thus, my question: Can one call a function on each member of a vector/matrix and return the result in a new vector/matrix all at once without using explicit loops?

That is I am looking for something like this:

 function retval = newfun(v)
    retval = 42*(v^23); 
endfunction

Perhaps, it is just syntactic sugar, that is all, but still would be useful to know.

  • 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-05-13T23:53:59+00:00Added an answer on May 13, 2026 at 11:53 pm

    The function should look like this:

    function retval = gauss(v, a, b, c)
      retval = a*exp(-(v-b).^2/(2*c^2));
    

    I would recommend you to read MATLAB documentation on how to vectorize the code and avoid loops:

    Code Vectorization Guide

    Techniques for Improving Performance

    Also remember that sometime code with loops can be more clear that vectorized one, and with recent introduction of JIT compiler MATLAB deals with loops pretty well.

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

Sidebar

Related Questions

This problem came up in the real world, but I've translated it into a
This came up from this answer to a previous question of mine . Is
I came from Java, and now I am working more with Ruby. One language
Came into the office today to do some more work on my nearly completed
I came into IOC via Google Guice. And now I've been forced to use
Came across this error today. Wondering if anyone can tell me what it means:
I came across this article written by Andrei Alexandrescu and Petru Marginean many years
I came across a printed article by Bertrand Meyer where he states that tests
I came across this recently, up until now I have been happily overriding the
I came across a controller in an older set of code (Rails 1.2.3) that

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.