0

Consider the arbitrary function:

function myFunc_ = myFunc(firstInput, secondInput)
    myFunc_ = firstInput * secondInput;
end

Now imagine I want to map the above function to an array for the first input firstInput, while the second input secondInput is constant. For example, something like:

firstVariable = linspace(0., 1.);

plot(firstVariable, map(myFunc, [firstVariable , 0.1]))

where 0.1 is an arbitrary scalar value for the secondInput and firstVariable array is an arbitrary array for the firstInput.

I have looked into the arrayfun() function. However, I don't know how to include the constant variable. Plus it seems like the syntax between MATLAB and Octave are different, or maybe I'm mistaken. It is important for me to have a cross-compatible code that I can share with colleagues.

3
  • 1
    Maybe I misunderstand what you mean by map(fun, argv), but wouldn't plot(firstVariable, myFunc(firstVariable, 0.1)) suffice? Commented Mar 14, 2021 at 22:10
  • @Vicky the myFunc(firstVariable, 0.1)) returns a scalar in Octave. I don't know what it does on MATLAB though! Commented Mar 14, 2021 at 22:22
  • 2
    myFunc(firstVariable, 0.1) returns a vector in MATLAB!! What happens if you substitute an elementwise .* for the * in your function definition, does that make any difference? (octave.org/doc/v6.2.0/Broadcasting.html) Commented Mar 14, 2021 at 22:40

2 Answers 2

3

Assuming in the original function you were multiplying two scalars and you want to vectorise, then

function myFunc_ = myFunc(firstInput, secondInput)
    myFunc_ = firstInput .* secondInput;
end

should work just fine.

Then plot it directly:

plot( firstVariable, myFunc(firstVariable , 0.1) )
Sign up to request clarification or add additional context in comments.

8 Comments

That's what I'd suspected from reading the docs!
No, I'm afraid the actual code is not as simple as the example given in the original question. I think I have found the right syntax that works on Octave. I will post it below for those who end up here.
Maybe I am making a mistake here, but I'm implementing equation 26 on this paper. And I have indeed used the elementwise arithmetic operators in the actual code.
@Foad: There's no reason you wouldn't be able to trivially vectorize that equation. You'd compute each of the three components first, say r1, r2 and r3, which would all be arrays, and then you compute max(max(r1,r2),r3) to find the element-wise max of the three. Computing max([r1,r2,r3]) could result in a scalar.
@Foad: Change your tmp0 function as I indicate in my comment above.
|
1

I'm afraid the arbitrary examples given in the original question were too simplified and as a result, they do not represent the actual issue I'm facing with my code. But I did manage to find the right syntax that works inside Octave:

plot(firstVariable, arrayfun(@(tempVariable) myFunc(tempVariable, 0.1), firstVariable))

basically the

@(tempVariable) myFunc(tempVariable, 0.1)

creates what is so-called an anonymous function and the

arrayfun(<function>, <array>)

maps the function over the given array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.