Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: create matrix as a function of previous columns

Subject: create matrix as a function of previous columns

From: curt

Date: 16 Jul, 2008 17:11:10

Message: 1 of 4


I would like to create a matrix in which each column is a function of the
previous column. It is probably easiest to describe what I would like to
do with the following code :

M is 20 x 100 matrix
k1 is 20 x 1 vector of constants
k2 is 20 x 1 vector of constants
k3 is 20 x 1 vector of constants

The first column of M is 'primed' with values.

for c = 2:100; % step through each column of M starting at column 2
M(:, c) = (k1 .* M(:, c-1)) + (k2 .* M(:, c-1)) + k3;
end;

This portion of code is called several times (with larger dimensions) and
is causing quite a slowdown and I would like to vectorize it to improve
performance.

I've been trying to do it with combinations of cumsum() and cumprod() and
I've gotten close but can't get it quite right.

Any suggestions would be greatly appreciated.

Thank you,
Curt


Subject: Re: create matrix as a function of previous columns

From: Peter Boettcher

Date: 16 Jul, 2008 17:36:13

Message: 2 of 4

Curt <97wideglide@gmail.com> writes:

> I would like to create a matrix in which each column is a function of the
> previous column. It is probably easiest to describe what I would like to
> do with the following code :
>
> M is 20 x 100 matrix
> k1 is 20 x 1 vector of constants
> k2 is 20 x 1 vector of constants
> k3 is 20 x 1 vector of constants
>
> The first column of M is 'primed' with values.
>
> for c = 2:100; % step through each column of M starting at column 2
> M(:, c) = (k1 .* M(:, c-1)) + (k2 .* M(:, c-1)) + k3;
> end;
>
> This portion of code is called several times (with larger dimensions) and
> is causing quite a slowdown and I would like to vectorize it to improve
> performance.
>
> I've been trying to do it with combinations of cumsum() and cumprod() and
> I've gotten close but can't get it quite right.

You could start with eliminating the excess operations. k1 and k2
associate, so:

M(:,c) = (k1+k2) .* M(:,c-1) + k3;

Maybe you could write down the closed-form expansion of this expression
and compute it directly?

kM + c
(kkM + kc) + c
(kkkM + kkc + kc) + c

I'm not doing this carefully, but I think it comes out to (for the jth column)

Mk.^j + sum_i=0^{j-1} c k^i

Mk.^j + c (k^j - 1) / (k^(j-1) - 1)

or, back to your problem

M(:,j) = M(:,1).*(k1+k2).^j + (k3.^j - 1) ./ (k3.^(j-1) - 1)


So now you can repmat your k1,k2,k3 columns, generate a j matrix for the
whole thing, and do this all in one expression.

Looking at this again, I think I'm probably off by one on the j parameter.


-Peter

Subject: Re: create matrix as a function of previous columns

From: John D'Errico

Date: 16 Jul, 2008 17:55:03

Message: 3 of 4

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyod4xbuk2.fsf@G99-Boettcher.llan.ll.mit.edu>...
> Curt <97wideglide@gmail.com> writes:
>
> > I would like to create a matrix in which each column is a function of the
> > previous column. It is probably easiest to describe what I would like to
> > do with the following code :
> >
> > M is 20 x 100 matrix
> > k1 is 20 x 1 vector of constants
> > k2 is 20 x 1 vector of constants
> > k3 is 20 x 1 vector of constants
> >
> > The first column of M is 'primed' with values.
> >
> > for c = 2:100; % step through each column of M starting at column 2
> > M(:, c) = (k1 .* M(:, c-1)) + (k2 .* M(:, c-1)) + k3;
> > end;
> >
> > This portion of code is called several times (with larger dimensions) and
> > is causing quite a slowdown and I would like to vectorize it to improve
> > performance.
> >
> > I've been trying to do it with combinations of cumsum() and cumprod()
and
> > I've gotten close but can't get it quite right.
>
> You could start with eliminating the excess operations. k1 and k2
> associate, so:
>
> M(:,c) = (k1+k2) .* M(:,c-1) + k3;
>
> Maybe you could write down the closed-form expansion of this expression
> and compute it directly?
>
> kM + c
> (kkM + kc) + c
> (kkkM + kkc + kc) + c
>
> I'm not doing this carefully, but I think it comes out to (for the jth column)
>
> Mk.^j + sum_i=0^{j-1} c k^i
>
> Mk.^j + c (k^j - 1) / (k^(j-1) - 1)
>
> or, back to your problem
>
> M(:,j) = M(:,1).*(k1+k2).^j + (k3.^j - 1) ./ (k3.^(j-1) - 1)
>
>
> So now you can repmat your k1,k2,k3 columns, generate a j matrix for the
> whole thing, and do this all in one expression.
>
> Looking at this again, I think I'm probably off by one on the j parameter.
>
>
> -Peter

It looks like a call to bsxfun to compute the powers,
then a cumsum.

John

Subject: Re: create matrix as a function of previous columns

From: curt

Date: 17 Jul, 2008 05:35:16

Message: 4 of 4

On Wed, 16 Jul 2008 13:36:13 -0400, Peter Boettcher wrote:

On Wed, 16 Jul 2008 13:36:13 -0400, Peter Boettcher wrote:

> Curt <97wideglide@gmail.com> writes:
>
>> I would like to create a matrix in which each column is a function of
>> the previous column. It is probably easiest to describe what I would
>> like to do with the following code :
>>
>> M is 20 x 100 matrix
>> k1 is 20 x 1 vector of constants
>> k2 is 20 x 1 vector of constants
>> k3 is 20 x 1 vector of constants
>>
>> The first column of M is 'primed' with values.
>>
>> for c = 2:100; % step through each column of M starting at column 2
>> M(:, c) = (k1 .* M(:, c-1)) + (k2 .* M(:, c-1)) + k3;
>> end;
>>
>> This portion of code is called several times (with larger dimensions)
>> and is causing quite a slowdown and I would like to vectorize it to
>> improve performance.
>>
>> I've been trying to do it with combinations of cumsum() and cumprod()
>> and I've gotten close but can't get it quite right.
>
> You could start with eliminating the excess operations. k1 and k2
> associate, so:
>
> M(:,c) = (k1+k2) .* M(:,c-1) + k3;
>
> Maybe you could write down the closed-form expansion of this expression
> and compute it directly?
>
> kM + c
> (kkM + kc) + c
> (kkkM + kkc + kc) + c
>
> I'm not doing this carefully, but I think it comes out to (for the jth
> column)
>
> Mk.^j + sum_i=0^{j-1} c k^i
>
> Mk.^j + c (k^j - 1) / (k^(j-1) - 1)
>
> or, back to your problem
>
> M(:,j) = M(:,1).*(k1+k2).^j + (k3.^j - 1) ./ (k3.^(j-1) - 1)
>
>
> So now you can repmat your k1,k2,k3 columns, generate a j matrix for the
> whole thing, and do this all in one expression.
>
> Looking at this again, I think I'm probably off by one on the j
> parameter.
>
>
> -Peter

Thank you very much for your response. I made a small mistake in the
description of my problem which changed it slightly. Sadly, I've been
working on this all day (and night) and now to solve it I think I need a
solution to another related problem.

The mistake I made is that K1 is actually a 20 x 100 matrix of constants
instead of a vector so I have to consider each individual Kn instead of
just repmating K1 and raising to the jth power.

The corrected code is :

for c = 2:100; % step through each column of M starting at column 2
  M(:, c) = (k1(:, c-1) .* M(:, c-1)) + (k2 .* M(:, c-1)) + k3;
end;

and the closed form expansion then looks like :

M
K1M + c
K2K1M + K2c + c
K3K2K1M + K3K2c + K3c + c
K4K3K2K1M + K4K3K2c + K4K3c + K4c + c

and for the jth column

(Kn...K1M) + (Kn...K2c) + (Kn...K3c) + ... + Knc + c

The first operand (Kn...K1M) is straightforward but the others have me
puzzled.

So, the problem I think I need to solve is given a matrix of values how
can I produce another matrix who's elements are the product of the
current element and the element in the previous column plus 1 ?

the following matrix :

v1 v4 v7
v2 v5 v8
v3 v6 v9

would produce :

v1 v4(v1+1) v7(v4(v1+1) +1)
v2 v5(v2+1) v8(v5(v2+1) +1)
v3 v6(v3+1) v9(v6(v3+1) +1)

I would really appreciate any advice anyone has for me.

Thanks,
Curt

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics