|
|
vectorizing if loop
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
|
|
|
|
Reply
|
Jonathan
|
3/23/2010 6:37:23 PM |
|
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
|
|
|
|
Reply
|
dpb
|
3/23/2010 6:54:54 PM
|
|
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
|
|
|
|
Reply
|
Matt
|
3/23/2010 7:03:20 PM
|
|
Dear Jonathan!
> yB(i)=cos((Beta-10)*pi()/180);
BTW. "pi" does not need brackets.
Jan
|
|
0
|
|
|
|
Reply
|
Jan
|
3/23/2010 7:18:02 PM
|
|
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
|
|
|
|
Reply
|
Jonathan
|
3/23/2010 7:27:21 PM
|
|
"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
|
|
|
|
Reply
|
Bruno
|
3/23/2010 7:27:21 PM
|
|
|
5 Replies
293 Views
(page loaded in 0.382 seconds)
Similiar Articles: vectorizing if loop - comp.soft-sys.matlabG'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 Bet... instead of For loop - comp.soft-sys.matlabYou *will* have to use a for loop. You might hide that fact behind the voodoo of 'vectorization', but the for-loop will be there, nonetheless. Floating point problem (again) - comp.lang.asm.x86On 2002-02-17 Tim Prince reported in comp.lang.fortran that the Intel beta 6 compiler was able to vectorize 58 out of 122 loops in a vectorization benchmark available ... Order of operations in a vectorized function - comp.soft-sys ...- - - - - - - - - - - Yes, if you want some kind of element-by-element updating, you would not want to vectorize it. Moreover it would have to be just the right kind of loops to match the ... A fast Distance computation method?? - comp.soft-sys.matlab ...Anybody whos good with vectorizing code, kindly help me here.. I have an Mx3 matrix. ... of such a matrix will >be zero. > >Its pretty simple to implement with loops but ... Speed-up the reading of large binary files with complex structures ...The loop for the snapshot_counter is fast, but the loop regarding to the point ... of the parsing *without* nested for-loop (int Matlab language it's called "vectorizing ... comp.soft-sys.matlab - page 107Vectorizing 2 5 (11/9/2003 5:07:53 AM) I'm having a hard time vectorizing this code. Can anybody help or is it even possible to remove the while and for loops in this piece ... Insert variable into string - comp.soft-sys.matlab> "I am 30 years old" > So I could put a for loop from 10 to 30 like: > for n=10:1 ... for n=10:1:30 > disp('I am <n> years old') > end And you can even vectorize it ... something about using colon (:) in array - comp.lang.fortran ...In fact, I use and recommend the following options '-O2 -funroll-loops -ftree-vectorize -march=3Dnative' (with the added information that I have worked on gfortran ... comp.soft-sys.matlab - page 73for loop - sorry to post again 1 3 (10/30/2003 10:12:51 PM) I really need to know how ... Are there any way to vectorize this command for ijj = 1: length( y) tmp = strvcat ... Vectorization (parallel computing) - Wikipedia, the free encyclopediaThe general framework for loop vectorization is split into four stages: Prelude: Where the loop-independent variables are prepared to be used inside the loop. vectorizing if loop - comp.soft-sys.matlab | Computer GroupG'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 Bet... 7/23/2012 10:46:18 AM
|
|
|
|
|
|
|
|
|