COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### vectorizing if loop

• Email
• Follow

```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((Beta-10)*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?

Thanks for any help on this.
Jon
```
 0

See related articles to this posting

```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((Beta-10)*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((Beta-10)*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.

--
```
 0

```Look at the value produced by Beta>0.  Now how does an IF statement deal with non-scalar arguments?  (HINT:  read the help for IF)

Beta = [52 52 52 52 60 52 40 24 32 44];
yB = zeros(length(Beta),1);

for ii = 1:length(Beta)
if Beta(ii)>0 & Beta(ii)<10
yB(ii)=1;
elseif Beta(ii)>10 & Beta(ii)<63
yB(ii)=cos((Beta(ii)-10)*pi/180);
elseif Beta(ii)>63
yB(ii)=0.63;
end
end

Is there a FAQ on this one yet?  There should be!
```
 0

```Dear Jonathan!

>         yB(i)=cos((Beta-10)*pi()/180);

BTW. "pi" does not need brackets.

Jan
```
 0

```Thank you both for your comments. This helped tremendously.

Jon
"Matt Fig" <spamanon@yahoo.com> wrote in message <hob39o\$n84\$1@fred.mathworks.com>...
> Look at the value produced by Beta>0.  Now how does an IF statement deal with non-scalar arguments?  (HINT:  read the help for IF)
>
> Beta = [52 52 52 52 60 52 40 24 32 44];
> yB = zeros(length(Beta),1);
>
>
> for ii = 1:length(Beta)
>     if Beta(ii)>0 & Beta(ii)<10
>         yB(ii)=1;
>     elseif Beta(ii)>10 & Beta(ii)<63
>         yB(ii)=cos((Beta(ii)-10)*pi/180);
>     elseif Beta(ii)>63
>         yB(ii)=0.63;
>     end
> end
>
>
>
>
> Is there a FAQ on this one yet?  There should be!
```
 0

```"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hob45a\$8p6\$1@fred.mathworks.com>...
> Dear Jonathan!
>
> >         yB(i)=cos((Beta-10)*pi()/180);
>
> BTW. "pi" does not need brackets.
>

The bracket should be here: cos((Beta-10)*(pi/180))
so that the vector is scanned one time less when multiplying.

Bruno
```
 0

5 Replies
358 Views

Similar Articles

12/6/2013 8:12:02 AM
page loaded in 21970 ms. (0)

Similar Artilces:

generating 3d matrix in loop
Hey all, I have such a problem: I want to add new values to my 3d matrix in such a loop y=[1 2 3 4 5 6 7 8 9 10]' for j=4:length(y) X=zeros(10,3,length(y)); X(:,2,j)=18; %I just used 18 for simplicity (in original code i have here a 2nd loop defining X values but it works ok) end so for each j value I want to have a specified values in my final matrix. but at the end in the matrix there are saved values only for j=length(y) but not for other j values... why it overwrites the values for earlier j values? any ideas how to fix this? "Kara " <pink03@o2.pl> wrote in message news:i0atbk\$830\$1@fred.mathworks.com... > Hey all, > I have such a problem: > I want to add new values to my 3d matrix in such a loop > y=[1 2 3 4 5 6 7 8 9 10]' > for j=4:length(y) > X=zeros(10,3,length(y)); Note that at each iteration, this overwrites the existing contents of X with all zeros. Move this to immediately before you enter the FOR loop. *snip* -- Steve Lord slord@mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us