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 19:28:02 +0000 (UTC) Organization: The Mitre Corp Lines: 30 Message-ID: <ir6fc2$bqe$1@newscl01ah.mathworks.com> References: <ir5rei$906$1@newscl01ah.mathworks.com> <ir5t6q$e44$1@newscl01ah.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: www-02-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: newscl01ah.mathworks.com 1305919682 12110 172.30.248.47 (20 May 2011 19:28:02 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Fri, 20 May 2011 19:28:02 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 2318 Xref: news.mathworks.com comp.soft-sys.matlab:727914 "Andy" wrote in message <ir5t6q$e44$1@newscl01ah.mathworks.com>... > "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 % Another way that may not require for loops or preallocation % (depending on the limits of rowben & colben % and once tester is calculated) is something like: pixelval = tester(:);