COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Add values to an array

• Follow

```Hi, I'm new in using Matlab.

X and Y is the centre point of my circle.
I want to store the value of x and y which will be random numbers. So i use the rand function.

I want to do this for N number of times, so how can i store ALL the values for x and y in an array, not just the LAST value of x and y?

This is important because i want to check if the distance between my latest centre of circle with the all of the previous ones in order to avoid overlapping of the circles.

Any suggestions?
```
 0

```
"mmm " <muzzeinmlk@yahoo.com> wrote in message
news:ib8qk5\$aru\$1@fred.mathworks.com...
> Hi, I'm new in using Matlab.
>
> X and Y is the centre point of my circle.
> I want to store the value of x and y which will be random numbers. So i
> use the rand function.
>
> I want to do this for N number of times, so how can i store ALL the values
> for x and y in an array, not just the LAST value of x and y?
>
> This is important because i want to check if the distance between my
> latest centre of circle with the all of the previous ones in order to
> avoid overlapping of the circles.
>
> Any suggestions?

Don't generate the points one-at-a-time.  Generate all N centers at once.

N = 10;
xy = rand(N, 2);

If, for whatever reason, you MUST generate the centers one-at-a-time, don't
forget to preallocate.

N = 10;
xy = zeros(N, 2);
for whichcenter = 1:N
xy(whichcenter, :) = rand(1, 2);
% Do other work with xy(whichcenter, :)
end

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
http://www.mathworks.com

```
 0

1 Replies
445 Views

Similiar Articles:

7/24/2012 5:10:59 AM