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:
generate variable spaced vector

Subject: generate variable spaced vector

From: Hermano Cappa

Date: 6 Jan, 2011 12:51:05

Message: 1 of 9

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

Subject: generate variable spaced vector

From: Steven_Lord

Date: 6 Jan, 2011 14:43:12

Message: 2 of 9



"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
order for anyone to have a chance to help you accomplish your goal.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: generate variable spaced vector

From: Hermano Cappa

Date: 6 Jan, 2011 17:09:05

Message: 3 of 9

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
> order for anyone to have a chance to help you accomplish your goal.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: generate variable spaced vector

From: Steven_Lord

Date: 6 Jan, 2011 18:25:38

Message: 4 of 9



"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
state ALL your requirements.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: generate variable spaced vector

From: Hermano Cappa

Date: 7 Jan, 2011 10:43:04

Message: 5 of 9

Hi Steven,

This is what I was looking for!!! Thanks for your time and helpful answer.

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
> state ALL your requirements.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: generate variable spaced vector

From: Hermano Cappa

Date: 7 Jan, 2011 21:30:21

Message: 6 of 9

"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
> state ALL your requirements.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> 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?

Subject: generate variable spaced vector

From: Walter Roberson

Date: 7 Jan, 2011 21:44:59

Message: 7 of 9

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?

Subject: generate variable spaced vector

From: Hermano Cappa

Date: 7 Jan, 2011 22:12:04

Message: 8 of 9

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.

Subject: generate variable spaced vector

From: Hermano Cappa

Date: 7 Jan, 2011 22:45:07

Message: 9 of 9

"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

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