Message Boards Message Boards

0
|
6448 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

A faster alternative to table?

Posted 8 years ago

Hello

I was wondering if there is a faster alternative to table in the following example

a = RandomReal[{-1, 1}, 100000];
b = RandomReal[{-1, 1}, 100000];
t = Range[Length[a]];
xa = Table[{t[[i]], a[[i]]}, {i, 1, Length[a]}];
xb = Table[{t[[i]], b[[i]]}, {i, 1, Length[b]}];

The above code is just to create xa and xb (they are signals collected from an experiment). Now the piece of code that I am interesting in finding a faster version

er = Table[{xa[[i, 1]], Log@Abs[xa[[i, 2]] - xb[[i, 2]]]}, {i, 1, 
    Length@xa}];

In real signals there is a lot of Log[0] and Mathematica takes ages to finally compute er.

Many thanks

Ed

POSTED BY: Eduardo Mendes
4 Replies
Posted 8 years ago
In[1]:= a = RandomReal[{-1, 1}, 100000];

In[2]:= b = RandomReal[{-1, 1}, 100000];

In[3]:= (* notice that *)
Log[Abs[{x1, x2} - {y1, y2}]]

Out[3]= {Log[Abs[x1 - y1]], Log[Abs[x2 - y2]]}

In[4]:= Timing[er = Transpose@{Range[Length[a]], Log[Abs[a - b]]};]

Out[4]= {0.0156001, Null}

In[5]:= er[[1 ;; 5]]

Out[5]= {{1, -0.0678263}, {2, -0.753793}, {3, -0.00108664}, {4, 
  0.419092}, {5, -2.70754}}

Also, it is fastest to give Mathematica real values:

In[7]:= zeros = Table[0, {1000000}];

In[9]:= Timing[Log[zeros];]

Out[9]= {0.374402, Null}

In[10]:= ones = Table[1, {1000000}];

In[11]:= Timing[Log[ones];]

Out[11]= {0.312002, Null}

In[12]:= realOnes = Table[1., {1000000}];

In[13]:= Timing[Log[realOnes];]

Out[13]= {0., Null}
POSTED BY: David Keith
Posted 8 years ago

Hi Michael

The problem is that a, b and t are not available. However the Transpose did the job.

a = {xa[[All, 1]], Log@Abs[xa[[All, 2]] - xb[[All, 2]]]} // Transpose;

Many thanks.

Regards, Ed

POSTED BY: Eduardo Mendes
Posted 8 years ago
In[1]:= $Version

Out[1]= "10.3.1 for Microsoft Windows (64-bit) (December 9, 2015)"

In[2]:= a = RandomReal[{-1, 1}, 100000];
b = RandomReal[{-1, 1}, 100000];
t = Range[Length[a]];
xa = Table[{t[[i]], a[[i]]}, {i, 1, Length[a]}];
xb = Table[{t[[i]], b[[i]]}, {i, 1, Length[b]}];

In[7]:= Timing[
 er = Table[{xa[[i, 1]], Log@Abs[xa[[i, 2]] - xb[[i, 2]]]}, {i, 1, 
     Length@xa}];]

Out[7]= {0.5625, Null}

In[8]:= Timing[er2 = {t, Log[Abs[a - b]]}\[Transpose];]

Out[8]= {0.015625, Null}

In[9]:= er == er2

Out[9]= True

Regards, Michael

POSTED BY: Michael Helmle

You should try to stay away from using Table when you don't have to:

a = RandomReal[{-1, 1}, 10^5];
b = RandomReal[{-1, 1}, 10^5];

t = Range[Length[a]];
xa = Transpose[{t, a}];
xb = Transpose[{t, b}];

er = {t, Log[Abs[a - b]]}\[Transpose];

I'm not sure why you need xa and xb, but those can also be replaced by a simple transpose. Similar answer as Michael. Should be fast.

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract