Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Adding values into a vector

Subject: Adding values into a vector

From: Timothy Westmaas

Date: 20 May, 2011 13:48:02

Message: 1 of 4

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.

Subject: Adding values into a vector

From: Andy

Date: 20 May, 2011 14:18:02

Message: 2 of 4

"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

Subject: Adding values into a vector

From: Timothy Westmaas

Date: 20 May, 2011 17:45:21

Message: 3 of 4

Andy, thanks so much for the quick reply!!

Subject: Adding values into a vector

From: someone

Date: 20 May, 2011 19:28:02

Message: 4 of 4

"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(:);

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us