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:
matlab alternative to mtimesx for this problem ?

Subject: matlab alternative to mtimesx for this problem ?

From: Juliette Salexa

Date: 28 Aug, 2011 18:58:10

Message: 1 of 12

Hello,

I want to integrate a function of w and t, wrt w, so the final answer should be a function of t.

Rather than integrating wrt w, for each t in a loop, I tried three 'vectorized' ways:

w=0:0.0001:1;t=0:0.0002:2;
tic;a=trapz(w,repmat(w,length(t),1).*repmat(t',1,length(w)),2);toc;
tic;b=trapz(w,bsxfun(@times,w,t'),2);toc;
tic;c=trapz(w,mtimesx(w,'T',t),2);toc;
isequal(a,b) % 1
isequal(a,c) % 1

The first two take up 4GB of RAM while computing, and take more than 50 seconds.
The third method takes negligible RAM and only 0.003245 seconds.

I only recently learned about James Tursa's (amazing) NTIMESX (thanks to Bruno and Matt) , and I happened to learn about it due to a completely unrelated reason to this problem .... this result explained above seems almost too good to be true !

Assuming that what I am doing with MTIMESX here is actually giving the answer I want (and this seems true since ISEQUAL gave me 1) , is there a better MATLAB way to do this ? The reason why I'm asking is because I'm actually writing this code for a different group and I'm not sure if they have MTIMESX or even a C compiler.

Subject: matlab alternative to mtimesx for this problem ?

From: Bruno Luong

Date: 28 Aug, 2011 21:15:31

Message: 2 of 12

"Juliette Salexa" wrote in message <j3e342$dbb$1@newscl01ah.mathworks.com>...
> Hello,
>
> I want to integrate a function of w and t, wrt w, so the final answer should be a function of t.
>
> Rather than integrating wrt w, for each t in a loop, I tried three 'vectorized' ways:
>
> w=0:0.0001:1;t=0:0.0002:2;
> tic;a=trapz(w,repmat(w,length(t),1).*repmat(t',1,length(w)),2);toc;
> tic;b=trapz(w,bsxfun(@times,w,t'),2);toc;
> tic;c=trapz(w,mtimesx(w,'T',t),2);toc;
> isequal(a,b) % 1
> isequal(a,c) % 1
>

Wait a minute, why you need to do thing so inefficiently? Sometime you just have to think harder than what you really computes.

The above results are equal to (at numerical precision)

> d = trapz(w,w)*t(:);

Try this to convince yourself:

w=rand(1,10)
t=rand(1,4)
trapz(w,repmat(w,length(t),1).*repmat(t',1,length(w)),2)
trapz(w,w)*t(:)

Which will be ridiculously inexpensive in memory and times.

BTW:
repmat(w,length(t),1).*repmat(t',1,length(w)),
bsxfun(@times,w,t')
mtimesx(w,'T',t)

is simply:

> t'*w

Bruno

Subject: matlab alternative to mtimesx for this problem ?

From: Juliette Salexa

Date: 28 Aug, 2011 23:24:09

Message: 3 of 12

Merci Bruno,

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> > I want to integrate a function of w and t, wrt w, so the final answer should be a function of t.

> Wait a minute, why you need to do thing so inefficiently? Sometime you just have to think harder than what you really computes.
>
> The above results are equal to (at numerical precision)
>
> > d = trapz(w,w)*t(:);


This is obviously not true for all functions of w and t, and the example I gave wasn't actually the function I'm integrating, but a simpler example.

> BTW:
> repmat(w,length(t),1).*repmat(t',1,length(w)),
> bsxfun(@times,w,t')
> mtimesx(w,'T',t)
>
> is simply:
>
> > t'*w
>


Thank you.

But:
tic;d=trapz(w,t'*w,2);toc

takes 4GB of RAM and 52 seconds. (same as REPMAT and BSXFUN)

Subject: matlab alternative to mtimesx for this problem ?

From: Roger Stafford

Date: 29 Aug, 2011 00:17:10

Message: 4 of 12

> This is obviously not true for all functions of w and t, and the example I gave wasn't actually the function I'm integrating, but a simpler example.
> ........
- - - - - - - - - - -
  Juliette, could you please describe very carefully the actual operation you performed that took "only 0.003245 seconds"? As you described it at the beginning of this thread, that would represent a multiplication rate of more than 30 gigaflops even if the execution time for trapz is totally ignored. That sounds fishy to me to say the least. There is also the question of how trapz could possibly obtain an answer without making use of a full 4GB array.

  If I understand James Tursa's explanation of 'mtimesx', the expression

 mtimesx(w,'T',t)

applies the transpose operation to the first ('w') argument, not the second one. This would make it not the equivalent of the repmat and bsxfun arrays for general w and t. If you made a similar mistake on your actual test, perhaps you somehow produced a much smaller array that only coincidentally gave you a correct (and faster) answer.

Roger Stafford

Subject: matlab alternative to mtimesx for this problem ?

From: Juliette Salexa

Date: 29 Aug, 2011 00:41:11

Message: 5 of 12

"Roger Stafford" wrote in message <j3elq6$3jr$1@newscl01ah.mathworks.com>...
> Juliette, could you please describe very carefully the actual operation you performed that took "only 0.003245 seconds"? As you described it at the beginning of this thread, that would represent a multiplication rate of more than 30 gigaflops even if the execution time for trapz is totally ignored. That sounds fishy to me to say the least. There is also the question of how trapz could possibly obtain an answer without making use of a full 4GB array.

Roger you are 100% correct. I screwed up somewhere along the way and must have had the w and t arrays reset between timing the 2nd one and the third one.

I'm apologize to everyone that may have spent time thinking about that, for my mistake.

So would I be right in thinking that the best way to do this then by using a forloop over t rather than a possible 'vectorized' version (considering that I only have 64GB of RAM and will certainly need more if I'm using one of the above techniques) ?

Thank you.

Subject: matlab alternative to mtimesx for this problem ?

From: Roger Stafford

Date: 29 Aug, 2011 01:08:10

Message: 6 of 12

"Juliette Salexa" wrote in message <j3en77$7br$1@newscl01ah.mathworks.com>...
> .........
> So would I be right in thinking that the best way to do this then by using a forloop over t rather than a possible 'vectorized' version (considering that I only have 64GB of RAM and will certainly need more if I'm using one of the above techniques) ?
- - - - - - - - - -
  Your planned use of 'mtimesx' suggests that your integrand may be separable into two factors, one a function of w and the other a function of t. If so, as Bruno pointed out, the t-factor can be factored out of the integral and only a single integration need be performed.

  Otherwise I would suggest that rather than using ten thousand points in the range of w you consider a much smaller number and use one of the higher order integration methods to be found in the file exchange. As for the ten thousand points in t, you might consider reducing their number also and use interpolation to fill in the gaps after integration is performed if needed. This of course assumes that your integrand is a continuous function with continuous derivatives.

  Also if you are able to express the integrand as a function of w and t in an m-file or anonymous function, you might consider using one of matlab's quadrature functions, 'quad', etc. These will attempt to optimize the number of steps needed to achieve your desired accuracy rather than using a fixed number.

  Finally, don't overlook the possibility that your integral may have an analytic solution which the symbolic toolbox could find. That could greatly simplify the problem for you.

Roger Stafford

Subject: matlab alternative to mtimesx for this problem ?

From: Juliette Salexa

Date: 29 Aug, 2011 03:14:27

Message: 7 of 12

"Roger Stafford" wrote in message <j3eopq$bad$1@newscl01ah.mathworks.com>...

I appreciate your very helpful response but:

1) it can't be factored. I'm integrating something that contains cos(mtimesx(w,'T',t)) in it, among other things.

2) it can't be integrated analytically, I tried this first in mathematica

3) I'm fully aware of higher order techniques but what I was asking in my last message was whether or not a for loop over t would be better than any 'vectorized' version in the case where I need very fine meshes. The example I gave uses 4GB of RAM and in my real application t and w both go up to a factor of 100,000 more than they do in that example, so the memory problem will still be there if I used higher order techniques, especially since I have a very oscillatory integrand.

Subject: matlab alternative to mtimesx for this problem ?

From: Bruno Luong

Date: 29 Aug, 2011 06:20:24

Message: 8 of 12

"Juliette Salexa" wrote in message <j3f06j$12o$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <j3eopq$bad$1@newscl01ah.mathworks.com>...

> 3) I'm fully aware of higher order techniques but what I was asking in my last message was whether or not a for loop over t would be better than any 'vectorized' version in the case where I need very fine meshes. The example I gave uses 4GB of RAM and in my real application t and w both go up to a factor of 100,000 more than they do in that example, so the memory problem will still be there if I used higher order techniques, especially since I have a very oscillatory integrand.

You can for loop by block, each of them is still vectorized. I showed here how to do it previously, take a search in CSSM for "bruno MaxBufferSize"

Bruno

Subject: matlab alternative to mtimesx for this problem ?

From: Juliette Salexa

Date: 29 Aug, 2011 07:24:11

Message: 9 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message >
>You can for loop by block, each of them is still vectorized. I showed here how to do it >previously, take a search in CSSM for "bruno MaxBufferSize"

I'm not sure what CCSM is but I think I understand what you mean.
I can do something like trapz(w,t',w) for blocks of t that are not as big, and loop over all blocks of t .. and this way I'm not creating huge arrays but also I'm not looping as many times.

Thanks very much for the suggestion!

Subject: matlab alternative to mtimesx for this problem ?

From: Bruno Luong

Date: 29 Aug, 2011 07:45:13

Message: 10 of 12

"Juliette Salexa" wrote in message <j3feqr$9ra$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message >
> >You can for loop by block, each of them is still vectorized. I showed here how to do it >previously, take a search in CSSM for "bruno MaxBufferSize"
>
> I'm not sure what CCSM is but I think I understand what you mean.

CSSM stands for comp.soft-sys.matlab the board you are reading and posting.

Bruno

Subject: matlab alternative to mtimesx for this problem ?

From: Juliette Salexa

Date: 29 Aug, 2011 20:24:27

Message: 11 of 12

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
> CSSM stands for comp.soft-sys.matlab the board you are reading and posting.
>
> Bruno

Okay - Now I know =)

Subject: matlab alternative to mtimesx for this problem ?

From: dpb

Date: 29 Aug, 2011 20:30:56

Message: 12 of 12

On 8/29/2011 3:24 PM, Juliette Salexa wrote:
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>> CSSM stands for comp.soft-sys.matlab the board you are reading and
>> posting.
>>
>> Bruno
>
> Okay - Now I know =)

Except cssm isn't a "board"; it's a usenet(+) news group that TMW has
written a frontend web gui for (a la GoogleNews but at least functional
whereas GNews is pretty much useless for anything other than long
archives :( )...

(+) If this means nothing to you, then you might glance at

<http://en.wikipedia.org/wiki/Usenet>

--

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