Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Help with a for if loop
Replies: 6   Last Post: Nov 7, 2013 8:48 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Curious

Posts: 2,006
Registered: 12/6/04
Re: Help with a for if loop
Posted: Nov 6, 2013 5:26 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"Terrell" wrote in message <l5eei9$ks7$1@newscl01ah.mathworks.com>...
> "Terrell" wrote in message <l5e9h3$1tu$1@newscl01ah.mathworks.com>...
> > I am trying to make a for if loop work in matlab but am having no luck. It is as if the for loop runs once and then stops. The code is as follows:
> >
> > fs=4000;
> > Ts=1/fs;
> > N1=241;
> > tb=.055;
> > te=.115;
> > Ta=.060;
> > T=.050;
> > A=10;
> > fc=1000;
> > theta=0;
> > for t=-1:.001:1;
> > if (T<=t) && (t>=0)
> > g2=A*cos(2*pi*fc*t+theta);
> > else
> > g2=0;
> > end
> > if (Ta<=t) && (t>=(T+Ta))
> > g1=A*cos(2*pi*fc*(t-Ta)+theta);
> > else
> > g1=0;
> > end
> > end
> >
> > No matter what I do I end up with a 1 for t and 10 for g1 and g2. I can't seem to figure out how to get past this. Any help is appreciated,

>
> I guess I should have been more clear also. At the completion of the loop I need to have a matrix for t values and a matrix for the functions g1 and g2. The original purpose of the for if loop was to apply a multiplication by a shifted rect function. But I definately see the points you are both making about overwriting the t an g1 and g2 values. Should I use some sort of indexing to build a matrix via a loop or is there an easier way to do all of this?


dbp showed you how do do it "the right way."
A quick & dirty method would simply be to add indx = 1 before the loop,
replace your g1 & g2 in the loop with g1(indx) & g2(indx) and add
indx = indx + 1 at the end of (within) the loop.

To be more efficient, you should preallocate g1 & g2 before the loop.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.