From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Adding values into a vector
Date: Fri, 20 May 2011 14:18:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 23
Message-ID: <ir5t6q$e44$>
References: <ir5rei$906$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1305901082 14468 (20 May 2011 14:18:02 GMT)
NNTP-Posting-Date: Fri, 20 May 2011 14:18:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1895050
Xref: comp.soft-sys.matlab:727866

"Timothy Westmaas" <> wrote in message <ir5rei$906$>...
> I want to create a vector of say 20 values.  Basically, I have a for loop which runs 20 times.  In each iteration, a specific value is calculated, and this is the value that I want to add to the vector.
> At the moment, the final value is the only value that appears in my workspace, but I would like to have all 20 values.
> here's the piece of the code which pertains to the question:
> pixelval = tester(rowben,colben) 
> so as you can see, I'm getting the value of "tester" at a specific row and column coordinate, which was deteremined before.
> the variable 'pixelval' is inside of the for loop, and for every iteration, I would like its value to be placed into the vector.
> Is there any way that I can do this?
> Hopefully I've explained this properly.

You are overwriting pixelval on each loop iteration.  Instead, preallocate an array called pixelval to hold all of the data and index into it on each iteration of the loop, as follows:

pixelval = zeros(20,1); % preallocate
for ix=1:20
    pixelval(ix) = tester(rowben,colben);