How to vectorize these codes
2 views (last 30 days)
these days i am dosing some modification on my code with vectorization. For the following codes:
I vectorlize it, but when m equal to n, the results is NAN. So please help me on vectorlizing it.
[Code formated, Jan - Please format your code in the future, thanks]
Jan on 5 Nov 2012
Edited: Jan on 6 Nov 2012
There is no loop and therefore it is not trivial to guess, which operation you want to be vectorized.
[EDITED] Now your problem got clear, thanks.
function y = gam(a,m,n)
y = zeros(size(m));
index = (m == 0 & n ~= m);
y(index) = -a*(-1+(-1).^n(index)) ./ (n(index)*pi);
index = (m ~= 0 & n ~= m);
y(index) = a.*n(index).*(-1+(-1).^(m(index)+n(index))) ./ ...
(pi*(m(index).^2 - n(index) .^ 2));
Not tested because I cannot run Matlab currently.
But this is a guess only. I cannot conclude what the vectorized version of "n == m" should be. Is it "ismember(n, m)" or "n == m" or "any(n == m, 2)" etc?