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

### generate variable spaced vector

• Follow

```Hi,

I want to generate a spaced vector but the standard linspace and logspace functions doesn't met my requirements. The spacing must be variable between 'a' and 'b'. The begin and end spacing of the vector is also defined and has to go from this start spacing gradually to the end spacing. The spacing has to reduce from the start spacing to the end spacing. The number of spaced points is also given. How can I program this? A small example to clarify my question:

a = 0
b = 10
delta_start = 1
delta_end = 0.2
n = 20

variable spaced vector = 0   1   1.9  2.7  3.4   .....        9.8 10
```
 0

```
"Hermano Cappa" <cappasteven@gmail.com> wrote in message
news:ig4dro\$fmj\$1@fred.mathworks.com...
> Hi,
>
> I want to generate a spaced vector but the standard linspace and logspace
> functions doesn't met my requirements. The spacing must be variable
> between 'a' and 'b'. The begin and end spacing of the vector is also
> defined and has to go from this start spacing gradually to the end
> spacing. The spacing has to reduce from the start spacing to the end
> spacing. The number of spaced points is also given. How can I program
> this? A small example to clarify my question:
>
> a = 0
> b = 10
> delta_start = 1
> delta_end = 0.2
> n = 20
>
> variable spaced vector = 0   1   1.9  2.7  3.4   .....        9.8 10

a = 0
b = 10
delta_start = 1
delta_end = 0.2
n = 20
delta = delta_end:0.1:delta_start;
vsv = cumsum([a delta(end:-1:1)])

This appears to be close to what you're looking for, but it doesn't end at
10 and probably doesn't generalize.  You need to specify EXACTLY what
happens in the section of the vector that you've omitted via ellipsis in

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

```
 0

```Steven,

Thank you for your answer but i think you misunderstand a little bit my question.
The spacing between the values is variable, it shouldn't be 0.1. I took this only as an example to clarify my problem. The spacing is dependent on the input values so that it meet the requirements namely that:
a = 0
b = 10
delta_start = 1
delta_end = 0.2
n = 20

So Matlab has to decide what the spacing should be to meet the requirements. The result can thus also be:

variable spaced vector = 0   1   1.92  2.63  3.56   .....        9.8 10

I want to make a non uniform 2D mesh with these values that starts with a spacing of 1 end ends with a spacing of 0.2. In between I want 18 lines and the spacing must gradually decrease from 1 to 0.2. I hope my question is now more clear to you.

"Steven_Lord" <slord@mathworks.com> wrote in message <ig4ke0\$p3l\$1@fred.mathworks.com>...
>
>
> "Hermano Cappa" <cappasteven@gmail.com> wrote in message
> news:ig4dro\$fmj\$1@fred.mathworks.com...
> > Hi,
> >
> > I want to generate a spaced vector but the standard linspace and logspace
> > functions doesn't met my requirements. The spacing must be variable
> > between 'a' and 'b'. The begin and end spacing of the vector is also
> > defined and has to go from this start spacing gradually to the end
> > spacing. The spacing has to reduce from the start spacing to the end
> > spacing. The number of spaced points is also given. How can I program
> > this? A small example to clarify my question:
> >
> > a = 0
> > b = 10
> > delta_start = 1
> > delta_end = 0.2
> > n = 20
> >
> > variable spaced vector = 0   1   1.9  2.7  3.4   .....        9.8 10
>
> a = 0
> b = 10
> delta_start = 1
> delta_end = 0.2
> n = 20
> delta = delta_end:0.1:delta_start;
> vsv = cumsum([a delta(end:-1:1)])
>
> This appears to be close to what you're looking for, but it doesn't end at
> 10 and probably doesn't generalize.  You need to specify EXACTLY what
> happens in the section of the vector that you've omitted via ellipsis in
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> http://www.mathworks.com
```
 0

```
"Hermano Cappa" <cappasteven@gmail.com> wrote in message
news:ig4svh\$411\$1@fred.mathworks.com...
> Steven,
>
> Thank you for your answer but i think you misunderstand a little bit my
> question.
> The spacing between the values is variable, it shouldn't be 0.1. I took
> this only as an example to clarify my problem. The spacing is dependent on
> the input values so that it meet the requirements namely that:
> a = 0
> b = 10
> delta_start = 1
> delta_end = 0.2
> n = 20
>
> So Matlab has to decide what the spacing should be to meet the
> requirements. The result can thus also be:
>
> variable spaced vector = 0   1   1.92  2.63  3.56   .....        9.8 10
>
> I want to make a non uniform 2D mesh with these values that starts with a
> spacing of 1 end ends with a spacing of 0.2. In between I want 18 lines
> and the spacing must gradually decrease from 1 to 0.2. I hope my question
> is now more clear to you.

a = 0;
b = 10;
delta_start = 1;
delta_end =  0.2;
n = 20;

v = zeros(1, n);
v(1) = a;
v(2) = a+delta_start;
v(n) = b;
v(n-1) = b-delta_end;
remainingSpace = v(n-1)-v(2);
x = rand(1, n-3);
x = sort(x./sum(x), 'descend');
while x(1) > delta_start/remainingSpace | x(n-3) < delta_end/remainingSpace
x = rand(1, n-3);
x = sort(x./sum(x), 'descend');
end
for k = 3:n-2
v(k) = v(k-1)+remainingSpace*x(k-2);
end

If this still doesn't do what you want, you need to clearly and explicitly

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

```
 0

```Hi Steven,

Thank you very much!!!

"Steven_Lord" <slord@mathworks.com> wrote in message <ig51f2\$1o8\$1@fred.mathworks.com>...
>
>
> "Hermano Cappa" <cappasteven@gmail.com> wrote in message
> news:ig4svh\$411\$1@fred.mathworks.com...
> > Steven,
> >
> > Thank you for your answer but i think you misunderstand a little bit my
> > question.
> > The spacing between the values is variable, it shouldn't be 0.1. I took
> > this only as an example to clarify my problem. The spacing is dependent on
> > the input values so that it meet the requirements namely that:
> > a = 0
> > b = 10
> > delta_start = 1
> > delta_end = 0.2
> > n = 20
> >
> > So Matlab has to decide what the spacing should be to meet the
> > requirements. The result can thus also be:
> >
> > variable spaced vector = 0   1   1.92  2.63  3.56   .....        9.8 10
> >
> > I want to make a non uniform 2D mesh with these values that starts with a
> > spacing of 1 end ends with a spacing of 0.2. In between I want 18 lines
> > and the spacing must gradually decrease from 1 to 0.2. I hope my question
> > is now more clear to you.
>
> a = 0;
> b = 10;
> delta_start = 1;
> delta_end =  0.2;
> n = 20;
>
> v = zeros(1, n);
> v(1) = a;
> v(2) = a+delta_start;
> v(n) = b;
> v(n-1) = b-delta_end;
> remainingSpace = v(n-1)-v(2);
> x = rand(1, n-3);
> x = sort(x./sum(x), 'descend');
> while x(1) > delta_start/remainingSpace | x(n-3) < delta_end/remainingSpace
>     x = rand(1, n-3);
>     x = sort(x./sum(x), 'descend');
> end
> for k = 3:n-2
>     v(k) = v(k-1)+remainingSpace*x(k-2);
> end
>
> If this still doesn't do what you want, you need to clearly and explicitly
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> http://www.mathworks.com
```
 0

```"Steven_Lord" <slord@mathworks.com> wrote in message <ig51f2\$1o8\$1@fred.mathworks.com>...
>
>
> "Hermano Cappa" <cappasteven@gmail.com> wrote in message
> news:ig4svh\$411\$1@fred.mathworks.com...
> > Steven,
> >
> > Thank you for your answer but i think you misunderstand a little bit my
> > question.
> > The spacing between the values is variable, it shouldn't be 0.1. I took
> > this only as an example to clarify my problem. The spacing is dependent on
> > the input values so that it meet the requirements namely that:
> > a = 0
> > b = 10
> > delta_start = 1
> > delta_end = 0.2
> > n = 20
> >
> > So Matlab has to decide what the spacing should be to meet the
> > requirements. The result can thus also be:
> >
> > variable spaced vector = 0   1   1.92  2.63  3.56   .....        9.8 10
> >
> > I want to make a non uniform 2D mesh with these values that starts with a
> > spacing of 1 end ends with a spacing of 0.2. In between I want 18 lines
> > and the spacing must gradually decrease from 1 to 0.2. I hope my question
> > is now more clear to you.
>
> a = 0;
> b = 10;
> delta_start = 1;
> delta_end =  0.2;
> n = 20;
>
> v = zeros(1, n);
> v(1) = a;
> v(2) = a+delta_start;
> v(n) = b;
> v(n-1) = b-delta_end;
> remainingSpace = v(n-1)-v(2);
> x = rand(1, n-3);
> x = sort(x./sum(x), 'descend');
> while x(1) > delta_start/remainingSpace | x(n-3) < delta_end/remainingSpace
>     x = rand(1, n-3);
>     x = sort(x./sum(x), 'descend');
> end
> for k = 3:n-2
>     v(k) = v(k-1)+remainingSpace*x(k-2);
> end
>
> If this still doesn't do what you want, you need to clearly and explicitly
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> http://www.mathworks.com

Hi Steven,

I'm working with your code to generate the spacing of my mesh but I have one problem. If delta_start and/or delta_end are very small (1e-3), it takes to much iterations before the condition (x(1) > delta_start/remainingSpace | x(n-3) < delta_end/remainingSpace) is met. How can I force the iteration in the good direction to decrease the calculation time?
```
 0

```On 11-01-07 03:30 PM, Hermano Cappa wrote:

> I'm working with your code to generate the spacing of my mesh but I have one
> problem. If delta_start and/or delta_end are very small (1e-3), it takes to
> much iterations before the condition (x(1) > delta_start/remainingSpace |
> x(n-3) < delta_end/remainingSpace) is met. How can I force the iteration in
> the good direction to decrease the calculation time?

You had posted the closely related "reverse" log spacing question, which I
posted a deterministic solution to under reasonable assumptions. Was that for
a different matter, or did you find that my rlogspace function did not
function correctly?

```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <ig81ir\$d1f\$2@nrc-news.nrc.ca>...
> On 11-01-07 03:30 PM, Hermano Cappa wrote:
>
> > I'm working with your code to generate the spacing of my mesh but I have one
> > problem. If delta_start and/or delta_end are very small (1e-3), it takes to
> > much iterations before the condition (x(1) > delta_start/remainingSpace |
> > x(n-3) < delta_end/remainingSpace) is met. How can I force the iteration in
> > the good direction to decrease the calculation time?
>
> You had posted the closely related "reverse" log spacing question, which I
> posted a deterministic solution to under reasonable assumptions. Was that for
> a different matter, or did you find that my rlogspace function did not
> function correctly?

Walter,

Thanks for your answer. Yes first I wanted to solve it with the logspace. But then I still had the problem that the spacing can be bigger than the requirement of delta_start and delta_end as explained above.
```
 0

```"Hermano Cappa" wrote in message <ig833k\$d14\$1@fred.mathworks.com>...
> Walter Roberson <roberson@hushmail.com> wrote in message <ig81ir\$d1f\$2@nrc-news.nrc.ca>...
> > On 11-01-07 03:30 PM, Hermano Cappa wrote:
> >
> > > I'm working with your code to generate the spacing of my mesh but I have one
> > > problem. If delta_start and/or delta_end are very small (1e-3), it takes to
> > > much iterations before the condition (x(1) > delta_start/remainingSpace |
> > > x(n-3) < delta_end/remainingSpace) is met. How can I force the iteration in
> > > the good direction to decrease the calculation time?
> >
> > You had posted the closely related "reverse" log spacing question, which I
> > posted a deterministic solution to under reasonable assumptions. Was that for
> > a different matter, or did you find that my rlogspace function did not
> > function correctly?
>
> Walter,
>
> Thanks for your answer. Yes first I wanted to solve it with the logspace. But then I still had the problem that the spacing can be bigger than the requirement of delta_start and delta_end as explained above.

Steven, Walter,

I solved my problem!

Thanks
```
 0

8 Replies
154 Views

Similiar Articles:

7/25/2012 5:59:36 PM