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:
How to create a matrix with increasing power?

Subject: How to create a matrix with increasing power?

From: Timothy Liang

Date: 21 Feb, 2014 20:49:12

Message: 1 of 9

I just started to learn how to use matlab... Just a simply question...

Is there a simple way create a matrix like this?
x= 1, 2, 3, 4, 5

matrix X=1^0 1^1 1^2 1^3
             2^0 2^1 2^2 2^3
             3...
             4...
             5...

Thanks!!

Subject: How to create a matrix with increasing power?

From: Sven

Date: 21 Feb, 2014 21:23:09

Message: 2 of 9

"Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> I just started to learn how to use matlab... Just a simply question...
>
> Is there a simple way create a matrix like this?
> x= 1, 2, 3, 4, 5
>
> matrix X=1^0 1^1 1^2 1^3
> 2^0 2^1 2^2 2^3
> 3...
> 4...
> 5...
>
> Thanks!!

Yep, try this:

bsxfun(@power, 0:5, (1:5)')
ans =

           0 1 2 3 4 5
           0 1 4 9 16 25
           0 1 8 27 64 125
           0 1 16 81 256 625
           0 1 32 243 1024 3125

Subject: How to create a matrix with increasing power?

From: Timothy Liang

Date: 21 Feb, 2014 21:59:08

Message: 3 of 9

"Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>...
> "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> > I just started to learn how to use matlab... Just a simply question...
> >
> > Is there a simple way create a matrix like this?
> > x= 1, 2, 3, 4, 5
> >
> > matrix X=1^0 1^1 1^2 1^3
> > 2^0 2^1 2^2 2^3
> > 3...
> > 4...
> > 5...
> >
> > Thanks!!
>
> Yep, try this:
>
> bsxfun(@power, 0:5, (1:5)')
> ans =
>
> 0 1 2 3 4 5
> 0 1 4 9 16 25
> 0 1 8 27 64 125
> 0 1 16 81 256 625
> 0 1 32 243 1024 3125

Thanks!!! It works!! I just need to transpose it!

Subject: How to create a matrix with increasing power?

From: someone

Date: 21 Feb, 2014 22:03:09

Message: 4 of 9

"Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>...
> "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> > I just started to learn how to use matlab... Just a simply question...
> >
> > Is there a simple way create a matrix like this?
> > x= 1, 2, 3, 4, 5
> >
> > matrix X=1^0 1^1 1^2 1^3
> > 2^0 2^1 2^2 2^3
> > 3...
> > 4...
> > 5...
> >
> > Thanks!!
>
> Yep, try this:
>
> bsxfun(@power, 0:5, (1:5)')
> ans =
>
> 0 1 2 3 4 5
> 0 1 4 9 16 25
> 0 1 8 27 64 125
> 0 1 16 81 256 625
> 0 1 32 243 1024 3125

I don't think this is what Timothy asked for.

If I understand the OP he wants a 4X5 matrix where given:

x = 1:5
y = 10^(0:3)

for ii = 1:length(x)
   X(:,ii) = x(ii)*y;
end

I don't have MATLAB installed on this computer,
but the result should be:
 X= [
1 1 2 3
1 2 4 8
1 3 9 27
1 4 16 64
1 5 25 125]

which is what I believe the OP asked for. (Maybe its transposed.)

I know there are more MATLAB efficient ways,
but Timothy did ask for a SIMPLE way.

Subject: How to create a matrix with increasing power?

From: someone

Date: 21 Feb, 2014 22:12:09

Message: 5 of 9


... snip ...
> but the result should be:
> X= [
> 1 1 2 3
> 1 2 4 8
> 1 3 9 27
> 1 4 16 64
> 1 5 25 125]
>
> which is what I believe the OP asked for. (Maybe its transposed.)
>
> I know there are more MATLAB efficient ways,
> but Timothy did ask for a SIMPLE way.

Actually, the above result should be:

> X= [
> 1 1 1 1
> 1 2 4 8
> 1 3 9 27
> 1 4 16 64
> 1 5 25 125]

Subject: How to create a matrix with increasing power?

From: John D'Errico

Date: 21 Feb, 2014 23:48:14

Message: 6 of 9

"someone" wrote in message <le8iet$dv5$1@newscl01ah.mathworks.com>...
> "Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>...
> > "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> > > I just started to learn how to use matlab... Just a simply question...
> > >
> > > Is there a simple way create a matrix like this?
> > > x= 1, 2, 3, 4, 5
> > >
> > > matrix X=1^0 1^1 1^2 1^3
> > > 2^0 2^1 2^2 2^3
> > > 3...
> > > 4...
> > > 5...
> > >
> > > Thanks!!
> >
> > Yep, try this:
> >
> > bsxfun(@power, 0:5, (1:5)')
> > ans =
> >
> > 0 1 2 3 4 5
> > 0 1 4 9 16 25
> > 0 1 8 27 64 125
> > 0 1 16 81 256 625
> > 0 1 32 243 1024 3125
>
> I don't think this is what Timothy asked for.
>
> If I understand the OP he wants a 4X5 matrix where given:
>
> x = 1:5
> y = 10^(0:3)
>
> for ii = 1:length(x)
> X(:,ii) = x(ii)*y;
> end
>
> I don't have MATLAB installed on this computer,
> but the result should be:
> X= [
> 1 1 2 3
> 1 2 4 8
> 1 3 9 27
> 1 4 16 64
> 1 5 25 125]
>
> which is what I believe the OP asked for. (Maybe its transposed.)
>
> I know there are more MATLAB efficient ways,
> but Timothy did ask for a SIMPLE way.

Geez. BSXFUN IS the simple way.

Admittedly, the answer given that used bsxfun was
wrong in the respect that the poster wanted a
slightly different result.

But no, the poster did explicitly ask for a 5x4 matrix,
NOT a 4x5 matrix.

bsxfun(@power, (1:5)',0:3)
ans =
     1 1 1 1
     1 2 4 8
     1 3 9 27
     1 4 16 64
     1 5 25 125

As far as the simple way, I find it difficult to understand
how a trivial (and clear) one line solution is not the
simpler solution than something that uses loops and
indexing.

Finally if you don't understand how to use bsxfun, then
it is high time to learn, as this is a powerful tool in
MATLAB.

John

Subject: How to create a matrix with increasing power?

From: someone

Date: 22 Feb, 2014 17:35:08

Message: 7 of 9

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <le8oju$s2t$1@newscl01ah.mathworks.com>...
> "someone" wrote in message <le8iet$dv5$1@newscl01ah.mathworks.com>...
> > "Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>...
> > > "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> > > > I just started to learn how to use matlab... Just a simply question...
> > > >
> > > > Is there a simple way create a matrix like this?
> > > > x= 1, 2, 3, 4, 5
> > > >
> > > > matrix X=1^0 1^1 1^2 1^3
> > > > 2^0 2^1 2^2 2^3
> > > > 3...
> > > > 4...
> > > > 5...
> > > >
> > > > Thanks!!
> > >
> > > Yep, try this:
> > >
> > > bsxfun(@power, 0:5, (1:5)')
> > > ans =
> > >
> > > 0 1 2 3 4 5
> > > 0 1 4 9 16 25
> > > 0 1 8 27 64 125
> > > 0 1 16 81 256 625
> > > 0 1 32 243 1024 3125
> >
> > I don't think this is what Timothy asked for.
> >
> > If I understand the OP he wants a 4X5 matrix where given:
> >
> > x = 1:5
> > y = 10^(0:3)
> >
> > for ii = 1:length(x)
> > X(:,ii) = x(ii)*y;
> > end
> >
> > I don't have MATLAB installed on this computer,
> > but the result should be:
> > X= [
> > 1 1 2 3
> > 1 2 4 8
> > 1 3 9 27
> > 1 4 16 64
> > 1 5 25 125]
> >
> > which is what I believe the OP asked for. (Maybe its transposed.)
> >
> > I know there are more MATLAB efficient ways,
> > but Timothy did ask for a SIMPLE way.
>
> Geez. BSXFUN IS the simple way.
>
> Admittedly, the answer given that used bsxfun was
> wrong in the respect that the poster wanted a
> slightly different result.
>
> But no, the poster did explicitly ask for a 5x4 matrix,
> NOT a 4x5 matrix.
>
> bsxfun(@power, (1:5)',0:3)
> ans =
> 1 1 1 1
> 1 2 4 8
> 1 3 9 27
> 1 4 16 64
> 1 5 25 125
>
> As far as the simple way, I find it difficult to understand
> how a trivial (and clear) one line solution is not the
> simpler solution than something that uses loops and
> indexing.
>
> Finally if you don't understand how to use bsxfun, then
> it is high time to learn, as this is a powerful tool in
> MATLAB.
>
> John

John,

Like I said in my original post, I knew there was a more efficient way but I didn't have MATLAB installed on the computer I was on at the time. My main concern was to alert the OP that the solution proposed was not what he asked for. Maybe, in hindsight, I shouldn't have emphasized "simple" in my post.

Subject: How to create a matrix with increasing power?

From: Timothy Liang

Date: 22 Feb, 2014 20:07:06

Message: 8 of 9

"Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>...
> "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> > I just started to learn how to use matlab... Just a simply question...
> >
> > Is there a simple way create a matrix like this?
> > x= 1, 2, 3, 4, 5
> >
> > matrix X=1^0 1^1 1^2 1^3
> > 2^0 2^1 2^2 2^3
> > 3...
> > 4...
> > 5...
> >
> > Thanks!!
>
> Yep, try this:
>
> bsxfun(@power, 0:5, (1:5)')
> ans =
>
> 0 1 2 3 4 5
> 0 1 4 9 16 25
> 0 1 8 27 64 125
> 0 1 16 81 256 625
> 0 1 32 243 1024 3125

Thanks!!! It works!! I just need to transpose it!

Subject: How to create a matrix with increasing power?

From: Timothy Liang

Date: 22 Feb, 2014 20:23:09

Message: 9 of 9

"someone" wrote in message <lean4c$fnp$1@newscl01ah.mathworks.com>...
> "John D'Errico" <woodchips@rochester.rr.com> wrote in message <le8oju$s2t$1@newscl01ah.mathworks.com>...
> > "someone" wrote in message <le8iet$dv5$1@newscl01ah.mathworks.com>...
> > > "Sven " <svendotholcombe@gmaildot.com> wrote in message <le8g3t$7kg$1@newscl01ah.mathworks.com>...
> > > > "Timothy Liang" <timothy.tliang@gmail.com> wrote in message <le8e48$1uu$1@newscl01ah.mathworks.com>...
> > > > > I just started to learn how to use matlab... Just a simply question...
> > > > >
> > > > > Is there a simple way create a matrix like this?
> > > > > x= 1, 2, 3, 4, 5
> > > > >
> > > > > matrix X=1^0 1^1 1^2 1^3
> > > > > 2^0 2^1 2^2 2^3
> > > > > 3...
> > > > > 4...
> > > > > 5...
> > > > >
> > > > > Thanks!!
> > > >
> > > > Yep, try this:
> > > >
> > > > bsxfun(@power, 0:5, (1:5)')
> > > > ans =
> > > >
> > > > 0 1 2 3 4 5
> > > > 0 1 4 9 16 25
> > > > 0 1 8 27 64 125
> > > > 0 1 16 81 256 625
> > > > 0 1 32 243 1024 3125
> > >
> > > I don't think this is what Timothy asked for.
> > >
> > > If I understand the OP he wants a 4X5 matrix where given:
> > >
> > > x = 1:5
> > > y = 10^(0:3)
> > >
> > > for ii = 1:length(x)
> > > X(:,ii) = x(ii)*y;
> > > end
> > >
> > > I don't have MATLAB installed on this computer,
> > > but the result should be:
> > > X= [
> > > 1 1 2 3
> > > 1 2 4 8
> > > 1 3 9 27
> > > 1 4 16 64
> > > 1 5 25 125]
> > >
> > > which is what I believe the OP asked for. (Maybe its transposed.)
> > >
> > > I know there are more MATLAB efficient ways,
> > > but Timothy did ask for a SIMPLE way.
> >
> > Geez. BSXFUN IS the simple way.
> >
> > Admittedly, the answer given that used bsxfun was
> > wrong in the respect that the poster wanted a
> > slightly different result.
> >
> > But no, the poster did explicitly ask for a 5x4 matrix,
> > NOT a 4x5 matrix.
> >
> > bsxfun(@power, (1:5)',0:3)
> > ans =
> > 1 1 1 1
> > 1 2 4 8
> > 1 3 9 27
> > 1 4 16 64
> > 1 5 25 125
> >
> > As far as the simple way, I find it difficult to understand
> > how a trivial (and clear) one line solution is not the
> > simpler solution than something that uses loops and
> > indexing.
> >
> > Finally if you don't understand how to use bsxfun, then
> > it is high time to learn, as this is a powerful tool in
> > MATLAB.
> >
> > John
>
> John,
>
> Like I said in my original post, I knew there was a more efficient way but I didn't have MATLAB installed on the computer I was on at the time. My main concern was to alert the OP that the solution proposed was not what he asked for. Maybe, in hindsight, I shouldn't have emphasized "simple" in my post.

Thank you all so much!! I would say bsxfun is the simplest command, although I did not know quite well about it. But the loops and indexing solution is more understandable to me since, like I said, I just started to learn Matlab. :)

As for the size of the matrix, it in fact does not matter. My homework is to create an 10000by20 matrix in order to test how least square projection error increases as the power in the polynomial function increases... Anyway, I would have more questions as the programming process continues...

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