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 improve this with Matlab vectors

Subject: How to improve this with Matlab vectors

From: Jeff

Date: 13 Oct, 2011 05:40:30

Message: 1 of 5

Can someone give an example of how to write this using Matlab's vectorizing? I'm sure there's a better way than what I have, but I can't quite get it working. I tried fooling around with meshgrid and stuff, but I just keep getting wrong results or 'mtimes' and 'mpower' errors.

function xfrm=Fxfrm(N)
% N is positive integer (usually a power of 2).
    omega=exp(-2*pi*1i/N);
    xfrm=ones(N);
    for m=1:N
        for n=1:N
            xfrm(m,n)=omega^((m-1)*(n-1));
        end
    end
end

Subject: How to improve this with Matlab vectors

From: Bruno Luong

Date: 13 Oct, 2011 06:53:08

Message: 2 of 5

"Jeff" wrote in message <j75tke$i2n$1@newscl01ah.mathworks.com>...
> Can someone give an example of how to write this using Matlab's vectorizing? I'm sure there's a better way than what I have, but I can't quite get it working. I tried fooling around with meshgrid and stuff, but I just keep getting wrong results or 'mtimes' and 'mpower' errors.
>
> function xfrm=Fxfrm(N)
> % N is positive integer (usually a power of 2).
> omega=exp(-2*pi*1i/N);
> xfrm=ones(N);
> for m=1:N
> for n=1:N
> xfrm(m,n)=omega^((m-1)*(n-1));
> end
> end
> end

omega=exp(-2*pi*1i/N);
xfrm = omega.^((0:N-1)'*(0:N-1))

Subject: How to improve this with Matlab vectors

From: Jeff

Date: 14 Oct, 2011 09:26:14

Message: 3 of 5

Good. Thanks.

Now, for a different class, I need to raise the numbers 0 though N-1 to successive powers, 1 through N. Like this:

0 0 0 ...
1 1 1 1 ....
2 4 8 16 ....
3 9 27 81 ...
...
N-1^1 N-1^2 ...

I tried starting with vertical vector (0:N-1)'. But how do I make each column raised to the next power?

I tried
(0:N-1)'.^(0:N-1) does not work
((0:N-1)')^(0:N-1)

I could just write a loop, but that seems so inelegant.
Thanks.

Subject: How to improve this with Matlab vectors

From: John D'Errico

Date: 14 Oct, 2011 09:52:29

Message: 4 of 5

"Jeff" wrote in message <j78v7m$q52$1@newscl01ah.mathworks.com>...
> Good. Thanks.
>
> Now, for a different class, I need to raise the numbers 0 though N-1 to successive powers, 1 through N. Like this:
>
> 0 0 0 ...
> 1 1 1 1 ....
> 2 4 8 16 ....
> 3 9 27 81 ...
> ...
> N-1^1 N-1^2 ...
>
> I tried starting with vertical vector (0:N-1)'. But how do I make each column raised to the next power?
>
> I tried
> (0:N-1)'.^(0:N-1) does not work
> ((0:N-1)')^(0:N-1)
>
> I could just write a loop, but that seems so inelegant.
> Thanks.

help bsxfun

John

Subject: How to improve this with Matlab vectors

From: Matt J

Date: 14 Oct, 2011 18:16:29

Message: 5 of 5

"Jeff" wrote in message <j78v7m$q52$1@newscl01ah.mathworks.com>...
> Good. Thanks.
>
> Now, for a different class, I need to raise the numbers 0 though N-1 to successive powers, 1 through N. Like this:
>
> 0 0 0 ...
> 1 1 1 1 ....
> 2 4 8 16 ....
> 3 9 27 81 ...
> ...
> N-1^1 N-1^2 ...
>
> I tried starting with vertical vector (0:N-1)'. But how do I make each column raised to the next power?
===============

Another method, somewhat derived from the first case:

exp(log(0:N-1)'*(1:N-1))

Tags for 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