increse an array in a while loop

  • Follow


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:













7/24/2012 12:36:39 PM


Reply: