how can i increase an array in a while loop? I want each time my loop executed my array to increase one index...
For excample...
% an array
x=[....];
a=x(1);
c=[c1,c2]
while a< 10
a=a+x(the next value of this array e.g. x(2) )
% c is my array and i want in any execution of the loop to increase it and add a value in a new index... this value is the next value in the array x
e.g. c=[x(1),x(2)....]
Can anyone help me.... Please is urgent...
|
|
0
|
|
|
|
Reply
|
nonyt
|
12/28/2009 7:35:06 PM |
|
Not sure why you're using "a" or the while loop. Why don't you just
do
c=[c1 c2 x];
and be done with it?
With your way, you're adding values to a, not increasing the length (#
elements) of a. Plus your loop does nothing with x. Plus you say you
want c=[x(1),x(2)....] but what happened to the values of c1 and
c2??? Those constants seem to be dropped from the list for some
reason. If you really want that then just say "c=x;" and you're done,
or else do it the way I said and retain your initial elements of c1
and c2.
I suggest you just take another crack at it and you'll figure it out
in a few minutes. Or else come back here with a better formulated
question.
|
|
0
|
|
|
|
Reply
|
ImageAnalyst
|
12/28/2009 7:45:50 PM
|
|
"nonyt " <mariaki16@hotmail.com> wrote in message <hhb199$id1$1@fred.mathworks.com>...
> how can i increase an array in a while loop? I want each time my loop executed my array to increase one index...
> For excample...
> % an array
> x=[....];
> a=x(1);
> c=[c1,c2]
> while a< 10
> a=a+x(the next value of this array e.g. x(2) )
> % c is my array and i want in any execution of the loop to increase it and add a value in a new index... this value is the next value in the array x
> e.g. c=[x(1),x(2)....]
> Can anyone help me.... Please is urgent...
I'm not entirely clear on what exactly you're asking, but here's a way to keep track of how many times a loop has run:
index=0;
while a < 10
.... %yadda yadda yadda
index=index+1
end
index
will output the number of times the loop has run. The variable "index" starts at 0 before the loop runs and is increased by one each time the loop executes.
|
|
0
|
|
|
|
Reply
|
Vince
|
12/28/2009 7:48:03 PM
|
|
"nonyt " <mariaki16@hotmail.com> wrote in message <hhb199$id1$1@fred.mathworks.com>...
> how can i increase an array in a while loop? I want each time my loop executed my array to increase one index...
> For excample...
> % an array
> x=[....];
> a=x(1);
> c=[c1,c2]
> while a< 10
> a=a+x(the next value of this array e.g. x(2) )
> % c is my array and i want in any execution of the loop to increase it and add a value in a new index... this value is the next value in the array x
> e.g. c=[x(1),x(2)....]
> Can anyone help me.... Please is urgent...
% It sounds like you're trying to do the following:
iters=10;
for ix=1:iters
c(ix)=rand;
end
% This will append a random number to the end of the vector c with each iteration.
% However, this is terrible in practice. If you know how many iterations you
% will make, then you should preallocate space for c as follows:
iters=10;
c=zeros(1,iters);
for ix=1:iters
c(ix)=rand;
end
|
|
0
|
|
|
|
Reply
|
Andy
|
12/28/2009 7:59:05 PM
|
|
"Andy " <theorigamist@gmail.com> wrote in message <hhb2m9$gon$1@fred.mathworks.com>...
> "nonyt " <mariaki16@hotmail.com> wrote in message <hhb199$id1$1@fred.mathworks.com>...
> > how can i increase an array in a while loop? I want each time my loop executed my array to increase one index...
> > For excample...
> > % an array
> > x=[....];
> > a=x(1);
> > c=[c1,c2]
> > while a< 10
> > a=a+x(the next value of this array e.g. x(2) )
> > % c is my array and i want in any execution of the loop to increase it and add a value in a new index... this value is the next value in the array x
> > e.g. c=[x(1),x(2)....]
> > Can anyone help me.... Please is urgent...
Just to be clear i will tell you my real exersice cause i think i didn't explane well with my example....
x is an array of cards with values...
a is one card
and lets say b is my second card
c is my cards in an array... c=[a,b]
and i want this loop to check for the sum of the cards a+b if is < 10 to take the next card from the array x
i want each time to now the sum to check if is <10 to take another card and i want at then an array with the card i have....
my cards: c=[a,b.....]
for example if the loop executed...
c=[2,3,4,5,6]
my question is how i will increase this array in the loop and check the sum of the cards each time...
sum=a+b;
c=[a,b];
while sum<10
sum=sum+(the next card);
%all ways with the next card not the c... c then d then....(is not with letters is with values)
c=[a+b+c];
end
i hope now i am more clear...
tnx for the help in advance
|
|
0
|
|
|
|
Reply
|
nonyt
|
12/29/2009 1:54:03 PM
|
|
"nonyt " <mariaki16@hotmail.com> wrote in message <hhd1lr$dur$1@fred.mathworks.com>...
> "Andy " <theorigamist@gmail.com> wrote in message <hhb2m9$gon$1@fred.mathworks.com>...
> > "nonyt " <mariaki16@hotmail.com> wrote in message <hhb199$id1$1@fred.mathworks.com>...
> > > how can i increase an array in a while loop? I want each time my loop executed my array to increase one index...
> > > For excample...
> > > % an array
> > > x=[....];
> > > a=x(1);
> > > c=[c1,c2]
> > > while a< 10
> > > a=a+x(the next value of this array e.g. x(2) )
> > > % c is my array and i want in any execution of the loop to increase it and add a value in a new index... this value is the next value in the array x
> > > e.g. c=[x(1),x(2)....]
> > > Can anyone help me.... Please is urgent...
>
> Just to be clear i will tell you my real exersice cause i think i didn't explane well with my example....
> x is an array of cards with values...
> a is one card
> and lets say b is my second card
> c is my cards in an array... c=[a,b]
> and i want this loop to check for the sum of the cards a+b if is < 10 to take the next card from the array x
>
> i want each time to now the sum to check if is <10 to take another card and i want at then an array with the card i have....
>
> my cards: c=[a,b.....]
> for example if the loop executed...
>
> c=[2,3,4,5,6]
>
> my question is how i will increase this array in the loop and check the sum of the cards each time...
> sum=a+b;
> c=[a,b];
> while sum<10
> sum=sum+(the next card);
> %all ways with the next card not the c... c then d then....(is not with letters is with values)
> c=[a+b+c];
> end
>
> i hope now i am more clear...
> tnx for the help in advance
Okay, let's simplify the problem a little bit. You're thinking of x and c as actual piles of cards, and as a result you want to remove a card from x and add it to c. In reality, all you need to do is adjust where in x you are looking for the "next" card. Try something like this:
x=floor(5*rand(1,100)); % some random data; cards between 0 and 5
ix=2; % it sounds like you start with at least two cards; if not, change this
while sum(x(1:ix))<10
ix=ix+1;
end
Note: x(1:ix) is what you were calling c. It doesn't need to be stored separately.
|
|
0
|
|
|
|
Reply
|
Andy
|
12/29/2009 2:10:05 PM
|
|
|
5 Replies
200 Views
(page loaded in 0.106 seconds)
Similiar Articles: Skip lines in a for loop - comp.soft-sys.matlabHow to increase array size by 1 - comp.lang.idl-pvwave Issue with array length in for loop - comp.lang.java.help ... Skip lines in a for loop - comp.soft-sys.matlab Thanks ... retrieving the data from a cell array - comp.soft-sys.matlab ...I have a nonhomogeneous cell array "A" in which some cells are stored as for example A{1,1} while others are stored as for example A{1,1}{1,1}. when running a loop ... looping CSV.foreach?? - comp.lang.ruby... on the environmental performance of your products/services", "Improve ... lang.ruby Skip lines in a for loop - comp.soft-sys.matlab A for loop can run over any array ... Loop to evaluate consecutive values in array - comp.soft-sys ...I want to make a loop that will run through each value in the array and compare consecutive values. ... you'll also look at these help for; help while ... Concat array / loop - comp.soft-sys.sasConcat array / loop - comp.soft-sys.sas Concat string to cell array of strings - comp ... builds a string inside an iteration statement, such as a for or while loop, using ... Saving data in for loop - comp.soft-sys.matlabSaving value from for loop within while loop - comp.soft-sys ... (new to matlab ... Concat array / loop - comp.soft-sys.sas Saving data in for loop - comp.soft-sys.matlab ... For loops in BASIC - comp.sys.apple2.programmerSo I can use one loop in the code to count for all the variables in array I(x)? ... Saving value from for loop within while loop - comp.soft-sys ... ... Sending an array of astruct over a TCP socket (Using C). - comp ...... been having a problem with sending information over a TCP socket using an array of ... write(sd, buff, sizeof(rgbstruct) * 200 * 200); the receive is in a while loop reading ... GUI, interuption while loop - comp.soft-sys.matlabHi all, in GUI, I try to interuption for while loop...let's say ... to perform block ... to 2) of integer; variable Y : Y_array; begin while not endfile (data_in) loop ... grep improve performance - comp.unix.shell... many times grep command ( in loop ) on the same file ( big file ~ 7K lines ) Can I improve ... exec 3<${SourceFile} while read -u3 ... > ksh93 does allow associative arrays ... increse an array in a while loop - Newsreader - MATLAB Centralhow can i increase an array in a while loop? I want each time my loop executed my array to increase one index... For excample... % an array x=[....]; a=x(1); PHP Looping While loopsPHP Array PHP Calendar PHP Date PHP Directory ... do...while - loops through a block of code once, and ... is less than, or equal to 5. i will increase by 1 each time the loop ... 7/24/2012 12:36:39 PM
|