http://www.mathworks.com/matlabcentral/newsreader/view_thread/308063
MATLAB Central Newsreader  Adding values into a vector
Feed for thread: Adding values into a vector
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 20 May 2011 13:48:02 +0000
Adding values into a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308063#837064
Timothy Westmaas
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.<br>
<br>
At the moment, the final value is the only value that appears in my workspace, but I would like to have all 20 values.<br>
<br>
here's the piece of the code which pertains to the question:<br>
<br>
pixelval = tester(rowben,colben) <br>
<br>
so as you can see, I'm getting the value of "tester" at a specific row and column coordinate, which was deteremined before.<br>
<br>
the variable 'pixelval' is inside of the for loop, and for every iteration, I would like its value to be placed into the vector.<br>
<br>
Is there any way that I can do this?<br>
<br>
Hopefully I've explained this properly.

Fri, 20 May 2011 14:18:02 +0000
Re: Adding values into a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308063#837074
Andy
"Timothy Westmaas" <ece40270@myport.ac.uk> wrote in message <ir5rei$906$1@newscl01ah.mathworks.com>...<br>
> 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.<br>
> <br>
> At the moment, the final value is the only value that appears in my workspace, but I would like to have all 20 values.<br>
> <br>
> here's the piece of the code which pertains to the question:<br>
> <br>
> pixelval = tester(rowben,colben) <br>
> <br>
> so as you can see, I'm getting the value of "tester" at a specific row and column coordinate, which was deteremined before.<br>
> <br>
> the variable 'pixelval' is inside of the for loop, and for every iteration, I would like its value to be placed into the vector.<br>
> <br>
> Is there any way that I can do this?<br>
> <br>
> Hopefully I've explained this properly.<br>
<br>
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:<br>
<br>
pixelval = zeros(20,1); % preallocate<br>
for ix=1:20<br>
pixelval(ix) = tester(rowben,colben);<br>
end

Fri, 20 May 2011 17:45:21 +0000
Re: Adding values into a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308063#837115
Timothy Westmaas
Andy, thanks so much for the quick reply!!

Fri, 20 May 2011 19:28:02 +0000
Re: Adding values into a vector
http://www.mathworks.com/matlabcentral/newsreader/view_thread/308063#837122
someone
"Andy" wrote in message <ir5t6q$e44$1@newscl01ah.mathworks.com>...<br>
> "Timothy Westmaas" <ece40270@myport.ac.uk> wrote in message <ir5rei$906$1@newscl01ah.mathworks.com>...<br>
> > 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.<br>
> > <br>
> > At the moment, the final value is the only value that appears in my workspace, but I would like to have all 20 values.<br>
> > <br>
> > here's the piece of the code which pertains to the question:<br>
> > <br>
> > pixelval = tester(rowben,colben) <br>
> > <br>
> > so as you can see, I'm getting the value of "tester" at a specific row and column coordinate, which was deteremined before.<br>
> > <br>
> > the variable 'pixelval' is inside of the for loop, and for every iteration, I would like its value to be placed into the vector.<br>
> > <br>
> > Is there any way that I can do this?<br>
> > <br>
> > Hopefully I've explained this properly.<br>
> <br>
> 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:<br>
> <br>
> pixelval = zeros(20,1); % preallocate<br>
> for ix=1:20<br>
> pixelval(ix) = tester(rowben,colben);<br>
> end<br>
<br>
% Another way that may not require for loops or preallocation<br>
% (depending on the limits of rowben & colben<br>
% and once tester is calculated) is something like:<br>
<br>
pixelval = tester(:);