Prev: jpeg encodercode in matlab
Next: reshape
From: Walter Roberson on 25 Mar 2010 10:53 Wayne King wrote: > "Mario Fatafehi" <coruba9(a)hotmail.com> wrote in message > <hof3hk$pes$1(a)fred.mathworks.com>... >> Walter Roberson <roberson(a)hushmail.com> wrote in message >> <hoeurm$nga$1(a)canopus.cc.umanitoba.ca>... >> > Mario Fatafehi wrote: >> > elseif 2< abs(x) <= 4 >> > > 2 < abs(x) will return either 0 (false) or 1 (true). You then >> check to > see if that 0 or 1 is <= 4, which of course it is. >> > > If you want to check to see if a number is in a range, code both >> halves > of the test, 2 < abs(x) && abs(x) <= 4 >> >> But why does it gives me repeated answer? > > Mario, why do you say it gives repeated answers? This code > > function [y,z] = lab2_15192572_fun(x) > if abs(x) < 2 > z = x^2 + 1; > y = x^2 - 1; > elseif 2< abs(x) <= 4 > z = 1/(x^2 + 1); > y = 1/(x^2 - 1); > else > z = cos(x); > y = sin(x); > end > only returns z=(0.8^2+1) and y=(0.8^2-1) when you input (-0.8) Mario did not have the semi-colons in the code so the values would have been displayed by the routine. And since he did not have semi-colons in the code, chances are that when he called the routine as [y, z] = RoutineName(x) that he did not put a semi-colon on the end of the call, so Matlab would have displayed the output variables after the call. |