jeudi 27 juillet 2017

How to vectorize matlab function input when NaN could be the input?

I wrote a function which I hope to have vectorized input and output.

function output = myfunction(input1,input2)


if input1 == 0
    output = equation1 ;
else
    output = equation2 ;
end

I test to have input1=0 and input1= 0.5 and my function myfunction can work right. The input input1 and input2 is actually a 3D matrix. I am trying to have more efficient computation instead of running in a loop elements by elements. If all my input1 is nonzero element, myfunction can work correctly. It seems like once I input a matrix, it cannot go through the if-statement correctly. My function will give me NaN for the element that has input1=0. How to make a change so that myfunction can take either a number and also a matrix in a if-statement. Thank you!

Aucun commentaire:

Enregistrer un commentaire