Jonathan wrote:
> G'day,
>
> I have the following matrix...
>
> Beta = [52 52 52 52 60 52 40 24 32 44];
> yB = zeros(1,length(Beta)).';
> for i = 1:length(Beta)
> if Beta>0 & Beta<10
> yB(i)=1;
> elseif Beta>10 & Beta<63
> yB(i)=cos((Beta10)*pi()/180);
> elseif Beta>63
> yB(i)=0.63;
> end
> end
>
> For starters, the if loop doesn't do anything (other than creating the
> initial matrix) and it is not clear to me why. I guess one approach to
> vectorizing the code is to eliminate the first and third if statements
> and use the find function instead. However, is there also something
> wrong with the second if statement?
Well, there is no such thing as an "if loop" for starters. If you
really meant that as written that may be your conceptual problem.
The if statement(s) _DO_ do, something, just not what you (apparently)
think.
Type
doc if
for details of what, precisely.
> yB = zeros(1,length(Beta)).';
yB = zeros(length(Beta),1);
for i = 1:length(Beta)
if Beta(i)>0 & Beta(i)<10
% and etc., will fix your problem using the for...end
Alternatively, study the following
Beta(Beta>10 & Beta<63) = cos((Beta10)*pi/180);
and salt to suit...
Also, another way to apply limits is something like
x = max(min(x,MaxVal),MinVal); % Study this, too...
It would be applied before the conversion above altho your sample vector
is all within the limits given.

