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

### Recursive loop

• Email
• Follow

```Hi,

I want to compute the following loop:

a=2
for k=0:100

x=a*x;

end

My problem is I only get the last value x(k=100).

How can I "collect" all the x(k) in a vector?

feelaex
```
 0

See related articles to this posting

```> Hi,
>
> I want to compute the following loop:
>
> a=2
> for k=0:100
>
>
>     x=a*x;
>
> end
>
> My problem is I only get the last value x(k=100).
>
> How can I "collect" all the x(k) in a vector?
>
> feelaex

a = 2;
n = 100;
x0 = 1.0;

x = zeros(n);

x(1) = x0 ;
For k = 1:(n-1)
x(k+1)=a*x(k);
end

Best wishes
Torsten.
```
 0

1 Replies
300 Views

Similar Articles

12/7/2013 9:44:02 AM
[PageSpeed]

Similar Artilces:

Variable in while loop not returning value outside the loop.?
#!/bin/bash total=0 echo -n "Enter part of filename: " read fnp ls -ld *\$fnp* 2> /dev/null | while read a b c d size e f g fname do [ ! -f \$fname ] && continue total=`expr \$total + \$size` echo \$total done echo Total size of files containing the pattern \"\$fnp\" is \$total _______________________________________________________________________ In the above code snippet, the last line always gives me value 0 for the variable total. But inside the loop the variable is having correct values (as understood with the help of echo inside the loop). Can anybody explain why \$total in the last line of the code is always 0. Also, Please suggest a solution for getting the value of \$total (as inside the loop) to be carried outside the loop too... Thanks. On 2006-06-13, Joï¿½ wrote: > #!/bin/bash > > total=0 > > echo -n "Enter part of filename: " > read fnp > > ls -ld *\$fnp* 2> /dev/null | while read a b c d size e f g fname > do > [ ! -f \$fname ] && continue > total=`expr \$total + \$size` > echo \$total > done > > echo Total size of files containing the pattern \"\$fnp

for loop pair-wise(?)
Hi there, I need to use each pair of 3 columns of a variable, 1-2, 1-3, 2-3 (fancy it's called combination) as inputs in a two-input function. Any idea how to get that into a for loop, if that is a good idea at all? If something like this was already discussed out there, I must have missed the right keywords. Thanks for helping. What is the variable? Is it a string with texts like '1-2' etc.? Or is it a 2 by 3 numerical array? Or is it a cell array? Give some code to create your sample data that people can use to try a little programming on. > What is Sorry, my wording was unclear, I should ould have said "matrix" or "array" instead of "variable". The question is how to get a pair of indexes in a for loop that are obtained by combination. E.g. for 1, 2 and 3 you have pairs 1-2, 1-3, 2-3. % or if you want M = rand(some dimension,3); for each of the pairs 1st and 2nd column, 1st and 3rd, 2nd and 3rd column of M do somefunction(x,y,...) end Hope it is now more clear. If someone could give a generalized idea (for N instead for 3), even much better. Thanks. "esozo " <esozo001@aol.de> wrote

Concatenate from a for loop
Hi, I have a for loop that calculates the position of a planet rotating about the sun. It uses ode45 and in iteration of the loop, calculates 11 values of x, y, xdot and ydot. This is done by initializing initial conditions and then resetting the initial conditions as the final conditions(11th) of each iteration of the loop. The code is as follows: y0 = [1;0;0;1]; z0 = [1.523;0;0;0.802]; w0=[1;0;0;1.09]; t0=0 tend=.1 step=tend/0.01 options=odeset('RelTol',1.e-7, 'AbsTol',1.e-9); for i=1:60 [t,z]=ode45(@twobodyfunctionmars,[0:0.01:tend],z0,options); [t,w]=ode45... and w0. My problem is that I can only manipulate the data of the final eleven valuues from the last iteration of the for loop. Im trying to concatenate each set of 11 values into a 4-d matrix conataining all values calculated over the entire loop. Could someone tell me a way of doing this so that I have all the values in one matrix that I can access and manipulate?? Thanks Brendan.

symbolic in FOR loop. not run...WHy??
Helloo all, I may put a question about symbolic & for loop. I AM USING FOR loop to construct functions as following: (M-FILE) syms x y for i=1:4 N(i,1)=(x-1)*(y-1);N(i+4,1)=(x-5+i)*(y-2); N(i,2)=-(x-i+1)*(y-1);N(i+4,2)=-(x-4+i)*(y-2); N(i,3)=(x-i+1)*(y);N(i+4,3)=(x-4+i)*(y-1); N(i,4)=-(x-1)*(y);N(i+4,4)=(x-5+i)*(y-1); end (RUNNING ERROR) ? The following error occurred converting from sym to double: Error using ==> sym.double at 25 DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead. Error in ==> poiosnfem_trial at 41 N(i,1)=(x-1)*(y-1);N(i+4,1)=(x-5+i)*(y-2); WHY?? I DONT UNDERSTAND AT ALL. please, ANY IDEAS! "Quynh Tram Nghuyen Thi" <lovelyeverything@yahoo.com.vn> wrote in message news:kb29gd\$ltb\$1@newscl01ah.mathworks.com... > Helloo all, > I may put a question about symbolic & for loop. I AM USING FOR loop to > construct functions as following: > > (M-FILE) > syms x y > for i=1:4 > N(i,1)=(x-1)*(y-1);N(i+4,1)=(x-5+i

For loop with matrices #3
Hi Everybody, Ok, here is my problem, i would like to execute a for loop with matrices, for instance, this program : k=input ('number = ') for x=1:k theta(x)=input('theta(x)=') m = cos(theta(x)*pi/180); n = sin(theta(x)*pi/180); T = [m*m n*n 2*m*n ; n*n m*m -2*m*n ; -m*n m*n m*m-n*n]; Tinv = [m*m n*n -2*m*n ; n*n m*m 2*m*n ; m*n -m*n m*m-n*n]; Qb = Tinv*Q*T end So basically, what i would like to do is calculating Qb from Qb1 to Qbk and save each value of Qb as Qb1,Qb2...Qbk. Thank you very much, Matthew

stuck in startup loop
I'm running Windows XP Home Edition on a newer Gateway computer. Have not loaded any new programs or done anything unusual. Turned it on the other day and got the screen that asks to start in normal, safe mode, etc. No matter which option I choose, it begins to load, then restarts and once again I get that same window. Any one have any ideas? Thanks. You might try rolling back the registry. WT "Brenda" <brendamatc@hotmail.com> wrote in message news:669f7190.0310021743.831296b@posting.google.com... > I'm running Windows XP Home Edition on a newer Gateway computer

How to access images in a loop
HI, My application needs to access nearly 100 images.The process is redundant hence logically it can be done in a loop.But i can't work out how to bring in the images in a loop.Is there a data structure similar to an array in 'C' to access images in a loop. "Raghuvarran" > My application needs to access nearly 100 images.The process is redundant hence logically it can be done in a loop.But i can't work out how to bring in the images in a loop... a hint: http://matlabwiki.mathworks.com/MATLAB_FAQ#How_can_I_process_a_sequence_of_files.3F us

redirect output from a for loop
Hi, I have the following 'for loop' to decompress a number of logs: for reqfile in \$FILEPATTERN do files=`ar -t \$reqfile | grep request` ar -p \$reqfile \${files} | gzip -dc done I want send ALL the output from the above into my perl script: report.pl But I'm not too sure how to do this. Could anyone help me out please? Many thanks, JS. In article <bn8fe1\$v0i\$1@cspc1n11.baplc.com>, JS wrote: > Hi, > > I have the following 'for loop' to decompress a number of logs: > > for reqfile in \$FILEPATTERN > do > files=`ar -t \$reqfile | grep request` > ar -p \$reqfile \${files} | gzip -dc > done > > I want send ALL the output from the above into my perl script: > > report.pl > > But I'm not too sure how to do this. Could anyone help me out please? > > Many thanks, in sh it would be something like this: for reqfile in \$FILEPATTERN do files=`ar -t \$reqfile | grep request` ar -p \$reqfile \${files} | gzip -dc done | report.pl In article <bn8fe1\$v0i\$1@cspc1n11.baplc.com>, JS wrote: > Hi, > > I have the following 'for loop' to decompress

using message loop for hotkey capturing
Hi all, but mainly Tim Golden: Tim, I am using your wonderful message loop for keyboard input, the one on your site that you pointed me to a few months ago. It has been working perfectly as long as I had only one dictionary of keys mapping to one dictionary of functions, but now I want two of each. My program has different modes, which may have varying keystrokes, and I also have some global keystrokes which are the same across all modes, like exiting or switching modes. I cannot figure out how to make the message loop look in two dictionaries at onc. I tried using an if, saying that if action_to_take was not set in the mode-specific dictionary then look at the global dictionary, but it is like it is never looking in the global dictionary at all. I get no syntax errors or problems when running the program, so it has to be something in my logic. Go to http://www.gateway2somewhere.com/sw/main.pyw to see what I mean; the problem code is near the very bottom of the file. Thanks for any suggestions. Oh, please note that I indent one space per indentation level. -- Have a great day, Alex (msg sent from GMail website) mehgcap@gmail.com; http://www.facebook.com

Do Loop in a macro numbers files and variables
and preq2success=1 then prereq=1; else prereq=0; if coursesuccess gt 0; run; proc freq data=&course&courseno&preq1&preq1no&preq2&preq2no noprint; where coursesuccess gt 0; table prereq*coursegrnum /nopercent out=freqcnt; run; %mend course2preq; How can I build a do loop to get rid of the repeated steps for preq2 and set it up so it can repeat n times, renumbering the output file each time (e.g., to produce output files entitled preq1, preq2, preq3, etc., each having variables with the filename digit in them (e.g., the third pass through the loop would produce...=subj; preq&I.crslev=crslev; preq&I.secx=secx; preq&I.grnum=offgrnum; preq&I.success=success; output preq&I ; end; %END; run; *Merges course and prerequisite datasets by pidm; * You could build a long dataset name here based on all Preqs, while macro looping; * but it might become too long. So a shorter rather fixed one has been choosen here; * If you want it differently, write the macro loop yourself, firstly creating a local; * empty macro variable, assigning it its own value concatenated with the additional; * name parts and so on. Name choosen