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:
Create a spaced vector for a log-plot

Subject: Create a spaced vector for a log-plot

From: Hermano Cappa

Date: 28 Apr, 2012 18:36:31

Message: 1 of 12

Hi,

I'm looking for a very simple manner to create a "log-spaced"vector which I want to use as the vector for my x-axis in a log-plot. I will also use this vector to evaluate my function at each of the value in this vector. I can not use the logspace command in Maltab because this give you something like: [1 10 100 1000 ...]

I want a vector with the following sequence:

[0.1 0.2 0.3 0.4 ... 1 2 3 4.... 10 20 30 40 .... 100 200 300 400 .... 1000]

How can I create this vector easily?

Thanks

Subject: Create a spaced vector for a log-plot

From: dpb

Date: 28 Apr, 2012 19:49:57

Message: 2 of 12

On 4/28/2012 1:36 PM, Hermano Cappa wrote:
> Hi,
>
> I'm looking for a very simple manner to create a "log-spaced"vector
> which I want to use as the vector for my x-axis in a log-plot. I will
> also use this vector to evaluate my function at each of the value in
> this vector. I can not use the logspace command in Maltab because this
> give you something like: [1 10 100 1000 ...]
>
> I want a vector with the following sequence:
>
> [0.1 0.2 0.3 0.4 ... 1 2 3 4.... 10 20 30 40 .... 100 200 300 400 ....
> 1000]
> How can I create this vector easily?
> Thanks

d1=-1;
d2=3;
l=[1:9].*10^d1;
for idx=d1+1,d2
   l=[l l.*idx^idx];
end

Wrap in a function as is/are lin/logspace and voila!

--

Subject: Create a spaced vector for a log-plot

From: Roger Stafford

Date: 28 Apr, 2012 21:53:13

Message: 3 of 12

dpb <none@non.net> wrote in message <jnhhl8$paq$1@speranza.aioe.org>...
> d1=-1;
> d2=3;
> l=[1:9].*10^d1;
> for idx=d1+1,d2
> l=[l l.*idx^idx];
> end
- - - - - - - - - -
  I think some modifications are needed there, dpb.

d1 = -1;
d2 = 2;
L = 10^d1;
for idx = d1:d2
 L = [L (2:10)*10^idx];
end

Roger Stafford

Subject: Create a spaced vector for a log-plot

From: dpb

Date: 28 Apr, 2012 23:00:03

Message: 4 of 12

On 4/28/2012 4:53 PM, Roger Stafford wrote:
> dpb <none@non.net> wrote in message <jnhhl8$paq$1@speranza.aioe.org>...
>> d1=-1;
>> d2=3;
>> l=[1:9].*10^d1;
>> for idx=d1+1,d2
>> l=[l l.*idx^idx];
>> end
> - - - - - - - - - -
> I think some modifications are needed there, dpb.
>
> d1 = -1;
> d2 = 2;
> L = 10^d1;
> for idx = d1:d2
> L = [L (2:10)*10^idx];
> end

Oh, yeah. I first wrote w/ the 1:9 in the expression then did the
change to a variable on the fly...Should have added the "salt to suit"
and "untested" caveats.

 >> l=[1:9].*10^d1;
 >> for idx=d1+1:d2,l=[l [1:9].*10^idx];end
 >> l'
ans =
     0.1000
     0.2000
     0.3000
     0.4000
     0.5000
     0.6000
     0.7000
     0.8000
     0.9000
     1.0000
     2.0000
     3.0000
     4.0000
...
     9.0000
    10.0000
    20.0000
    30.0000
    40.0000
...
    90.0000
   100.0000
   200.0000
...
   800.0000
   900.0000
 >>

Needs an end fixup if wants to end at next 10^(d2+1) instead of 9*10^d2
as this does. But, "salt to suit"... :)

--

Subject: Create a spaced vector for a log-plot

From: Roger Stafford

Date: 28 Apr, 2012 23:45:28

Message: 5 of 12

"Hermano Cappa" wrote in message <jnhdbf$nko$1@newscl01ah.mathworks.com>...
> Hi,
>
> I'm looking for a very simple manner to create a "log-spaced"vector which I want to use as the vector for my x-axis in a log-plot. I will also use this vector to evaluate my function at each of the value in this vector. I can not use the logspace command in Maltab because this give you something like: [1 10 100 1000 ...]
>
> I want a vector with the following sequence:
>
> [0.1 0.2 0.3 0.4 ... 1 2 3 4.... 10 20 30 40 .... 100 200 300 400 .... 1000]
>
> How can I create this vector easily?
>
> Thanks
- - - - - - - - -
  Hermano, you should realize that the spacing you have requested is not in any sense a "log-spaced vector" because the ratio between successive interval lengths is not held constant. From .1 to 1 the intervals are all the same length and then suddenly the length is multiplied by 10. The intervals are then constant again until 100, at which time they are again multiplied by 10, etc. This irregularity could lead to a misinterpretation of displayed data.

Roger Stafford

Subject: Create a spaced vector for a log-plot

From: dpb

Date: 29 Apr, 2012 14:19:28

Message: 6 of 12

On 4/28/2012 6:45 PM, Roger Stafford wrote:
...

> Hermano, you should realize that the spacing you have requested is not
> in any sense a "log-spaced vector" because the ratio between successive
> interval lengths is not held constant. From .1 to 1 the intervals are
> all the same length and then suddenly the length is multiplied by 10.
> The intervals are then constant again until 100, at which time they are
> again multiplied by 10, etc. This irregularity could lead to a
> misinterpretation of displayed data.

I figured he was looking to label ticks specifically; if so when he
switches/sets to log axes the spacing between the ticks will be set for
him automagically.

Certainly the values aren't logrithmically spaced numerically, that's as
OP noted what he gets if uses logspace() but they're "clean" for
presentation purposes and if the data are spaced over a reasonable
number of decades and plot size w/ font chosen appropriately, probably
makes for a decent-looking plot.

--

Subject: Create a spaced vector for a log-plot

From: Nasser M. Abbasi

Date: 29 Apr, 2012 14:45:14

Message: 7 of 12

On 04/28/2012 01:36 PM, Hermano Cappa wrote:
> Hi,
>
> I'm looking for a very simple manner to create a "log-spaced"vector which I want to use as the vector for my
 >x-axis in a log-plot. I will also use this vector to evaluate my
function at each of the value in this vector.
 >I can not use the logspace command in Maltab because this give you
something like: [1 10 100 1000 ...]
>
> I want a vector with the following sequence:
>
> [0.1 0.2 0.3 0.4 ... 1 2 3 4.... 10 20 30 40 .... 100 200 300 400 .... 1000]
>
> How can I create this vector easily?
>
> Thanks

may be one way:

-----------------------
z = -1:3; d=1:10;
cell2mat( arrayfun(@(i) d.*10^z(i),1:length(z),'UniformOutput',false) )
------------------------

--Nasser

Subject: Create a spaced vector for a log-plot

From: Nasser M. Abbasi

Date: 29 Apr, 2012 14:50:55

Message: 8 of 12

On 04/29/2012 09:45 AM, Nasser M. Abbasi wrote:
> On 04/28/2012 01:36 PM, Hermano Cappa wrote:
>> Hi,
>>
>> I'm looking for a very simple manner to create a "log-spaced"vector which I want to use as the vector for my
> >x-axis in a log-plot. I will also use this vector to evaluate my
> function at each of the value in this vector.
> >I can not use the logspace command in Maltab because this give you
> something like: [1 10 100 1000 ...]
>>
>> I want a vector with the following sequence:
>>
>> [0.1 0.2 0.3 0.4 ... 1 2 3 4.... 10 20 30 40 .... 100 200 300 400 .... 1000]
>>
>> How can I create this vector easily?
>>
>> Thanks
>
> may be one way:
>
> -----------------------
> z = -1:3; d=1:10;
> cell2mat( arrayfun(@(i) d.*10^z(i),1:length(z),'UniformOutput',false) )
> ------------------------
>

opps, that should be d=1:9 not d=1:10 in the above ! (just had a look
at the output I had :)

--Nasser

Subject: Create a spaced vector for a log-plot

From: dpb

Date: 29 Apr, 2012 15:03:27

Message: 9 of 12

On 4/29/2012 9:19 AM, dpb wrote:
...

> I figured he was looking to label ticks specifically; if so when he
> switches/sets to log axes the spacing between the ticks will be set for
> him automagically.
>
...
> ... they're "clean" for
> presentation purposes and if the data are spaced over a reasonable
> number of decades and plot size w/ font chosen appropriately, probably
> makes for a decent-looking plot.

Well, not so much... :(

At least in my old R14 release, set(gca,'xticklabel',l) w/ "l" being the
derived set of values puts them at the right locations but adds another
set of subticks that clutters things up. The font size needed is
_quite_ small for more than 2 decades as well.

Couldn't get it to work well w/ this release w/o more futzing than have
time for...or really inclination more accurately :)

I figured it would be far better than the results were... :(

--

Subject: Create a spaced vector for a log-plot

From: dpb

Date: 29 Apr, 2012 15:48:07

Message: 10 of 12

On 4/29/2012 9:50 AM, Nasser M. Abbasi wrote:
...

>> z = -1:3; d=1:10;
>> cell2mat( arrayfun(@(i) d.*10^z(i),1:length(z),'UniformOutput',false) )
>> ------------------------
>>
>
> opps, that should be d=1:9 not d=1:10 in the above ! (just had a look at
> the output I had :)
...

Indeed, I did that first in my approach... :)

I figured arrayfun would be a useful tool but it postdates my release so
I'm reluctant to try to wing it...

--

Subject: Create a spaced vector for a log-plot

From: Hermano Cappa

Date: 29 Apr, 2012 16:53:07

Message: 11 of 12

dpb <none@non.net> wrote in message <jnjiln$q5u$1@speranza.aioe.org>...
> On 4/28/2012 6:45 PM, Roger Stafford wrote:
> ...
>
> > Hermano, you should realize that the spacing you have requested is not
> > in any sense a "log-spaced vector" because the ratio between successive
> > interval lengths is not held constant. From .1 to 1 the intervals are
> > all the same length and then suddenly the length is multiplied by 10.
> > The intervals are then constant again until 100, at which time they are
> > again multiplied by 10, etc. This irregularity could lead to a
> > misinterpretation of displayed data.
>
> I figured he was looking to label ticks specifically; if so when he
> switches/sets to log axes the spacing between the ticks will be set for
> him automagically.
>
> Certainly the values aren't logrithmically spaced numerically, that's as
> OP noted what he gets if uses logspace() but they're "clean" for
> presentation purposes and if the data are spaced over a reasonable
> number of decades and plot size w/ font chosen appropriately, probably
> makes for a decent-looking plot.
>
> --

Indeed, dpb, the reason why I want to use this sequence of "log-spaced" values (indeed it is not a really log-spaced vector as Roger says) is only for a clean presentation in a log plot.

Subject: Create a spaced vector for a log-plot

From: dpb

Date: 29 Apr, 2012 17:40:45

Message: 12 of 12

On 4/29/2012 11:53 AM, Hermano Cappa wrote:
> dpb <none@non.net> wrote in message <jnjiln$q5u$1@speranza.aioe.org>...
...

>> I figured he was looking to label ticks specifically; if so when he
>> switches/sets to log axes the spacing between the ticks will be set
>> for him automagically.
...

> Indeed, dpb, the reason why I want to use this sequence of "log-spaced"
> values (indeed it is not a really log-spaced vector as Roger says) is
> only for a clean presentation in a log plot.

Well, good luck w/ that...didn't work well at all here w/ my early
release...see my followup on that.

--

Tags for this Thread

No tags are associated with 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