"I tried the following naive code but I only obtained a scalar and not the desired matrix"
You obtained a scalar because you assigned a scalar.
And you are using non-scalar value as a condition to if-else statements, which evaluates all the values to a condition and proceeds accordingly.
if [0 1; 1 2]
disp('if')
elseif 3*ones(5)
disp('elseif')
else
disp('else')
end
elseif
It depends on the operation but this should work for a good amount of cases. If this doesn't work, please specify the 'expensive operation' you are trying to implement.
Thanks for the answer. How to do expensive operations for these indices? Suppose that for r > t the function returns some complicated function of r and t that we denote g1(r,t). We would like to calculate g1(r,t) only when we are sure that r > t.
Also, for r < t, the function returns some complicated function of r and t that we denote g2(r,t). How to specify that already in the definition of the function f so as to avoid computing both g1 and g2 for all the values of r and t? I can simply make f return g1 .* (r > t) + g2 .* (r < t) but this would mean that the function f would have to compute g1 when r < t and g2 when r > t (very expensive operations). Any help is very much appreciated. Thank you,
g1 and g2 involve infinite sums of terms involving hypergeom functions. They are really very complicated and there is no real interest in providing their expressions explicitly here I guess. If we could have a solution for general g1 and g2, that would be a dream. Thanks Dyuman.
I realized that using parfor for parallel computation leads to a quick evaluation of the kernel functions without the need for vectorizing the implementation. Sometimes simple approaches save both time and energy. Thanks for your valuable help anyway.
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.