Path: news.mathworks.com!not-for-mail
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$1@newscl01ah.mathworks.com>
References: <ir5rei$906$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1305901082 14468 172.30.248.46 (20 May 2011 14:18:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 20 May 2011 14:18:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1895050
Xref: news.mathworks.com comp.soft-sys.matlab:727866

"Timothy Westmaas" <ece40270@myport.ac.uk> wrote in message <ir5rei$906$1@newscl01ah.mathworks.com>...
> 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);
end