Prev: Persistent webcam ID
Next: triggered single pulse
From: Gerrit Hofbauer on 11 Mar 2010 10:27 Hi, I have a function that looks like this: --- function f=CD(x) if x<2 f=24./x; elseif x<500 f=18.5./x.^0.6; else f=0.44; end --- As you can see the function has different function terms for different regions of x. If I call the function with x being a vector it only seems to evaluate the if-condition once for the highest value of that vector. Example1: >> CD(1) ans = 24 Example2: CD(1:3) ans = 18.5000 12.2054 9.5697 ---- So for Example2 matlab calculates the value CD(x=1)=18.5 instead of 24. Because of x=3 being the highest value of my input vector, it chooses f=18.5./x.^0.6 as my function for all elements of the x vector. I can work around this behavior if I call the function for every single element in a loop but actually I always considered matlab being a program that can handle calculations with vectors without using loops. Is there a way I can change the call of my function or the function itself so it works in the desired way (i.e. checking the condition for every single element of the vector)? Regards Gerrit
From: Matt J on 11 Mar 2010 10:54 "Gerrit Hofbauer" <gerrit.hofbauer(a)gmx.de> wrote in message <hnb25t$n8i$1(a)infosun2.rus.uni-stuttgart.de>... > As you can see the function has different function terms for different > regions of x. If I call the function with x being a vector it only seems to > evaluate the if-condition once for the highest value of that vector. No, if vector_expression will only evaluate true if real(vector_expression) is non-zero in all its elements. See "help if".
From: James Tursa on 11 Mar 2010 11:10 "Gerrit Hofbauer" <gerrit.hofbauer(a)gmx.de> wrote in message <hnb25t$n8i$1(a)infosun2.rus.uni-stuttgart.de>... > Hi, > > I have a function that looks like this: > > --- > function f=CD(x) > > if x<2 > f=24./x; > elseif x<500 > f=18.5./x.^0.6; > else > f=0.44; > end > --- > > As you can see the function has different function terms for different > regions of x. If I call the function with x being a vector it only seems to > evaluate the if-condition once for the highest value of that vector. > > > Example1: > > >> CD(1) > > ans = > > 24 > > > Example2: > > CD(1:3) > > ans = > > 18.5000 12.2054 9.5697 > > ---- > > So for Example2 matlab calculates the value CD(x=1)=18.5 instead of 24. > Because of x=3 being the highest value of my input vector, it chooses > f=18.5./x.^0.6 as my function for all elements of the x vector. > > I can work around this behavior if I call the function for every single > element in a loop but actually I always considered matlab being a program > that can handle calculations with vectors without using loops. > > Is there a way I can change the call of my function or the function itself > so it works in the desired way (i.e. checking the condition for every single > element of the vector)? > > Regards > > Gerrit The vector behavior needs to be present in the function itself. Otherwise you will have to use a loop in the calling routine. e.g., you could change your function code to something like this: xlt2 = x < 2; xlt500 = (~xlt2) & (x < 500); f = ones(size(x)) * 0.44; f(xlt2) = 24./x(xlt2); f(xlt500) = 18.5./x(xlt500).^0.6; All of that logical indexing does incur a memory and speed penalty which may be an issue for very large x. This could be avoided in the above case by using a C mex routine if necessary. James Tursa
From: Matt Fig on 11 Mar 2010 11:11 Instead of looping you could use logical indexing. This would look something like this: function f=CD(x) f = zeros(size(x)); idx1 = x<2; idx2 = x>=2 & x<500; idx3 = ~idx1 & ~idx2; f(idx1) = 24./x(idx1); f(idx2) = 18.5./x(idx2).^0.6; f(idx3) = .44;
From: Jan Simon on 11 Mar 2010 11:17
Dear Gerrit! > function f = CD(x) > > if x<2 > f=24./x; > elseif x<500 > f=18.5./x.^0.6; > else > f=0.44; > end Vectorized: function f = CD(x) f(1:length(x)) = 0.44; lt2 = x < 2; lt500 = and(x < 500, ~lt2); f(lt2) = 24 ./ x(lt2); f(lt500) = 18.5 ./ x(lt500) .^ 0.6; return; Good luck, Jan |