2

I am trying to generate a matrix in matlab which I will use to solve a polynomial regression formula.

Here is how I am trying to generate the matrix:

I have an input vector X containing N elements and an integer d. d is the integer to know how many times we will add a new column to the matrix we are trying to generate int he following way.

N = [X^d X^{d-1} ... X^2 X O]

O is a vector of same length as X with all 1's.

Everytime d > 2 it does not work.

Can you see any errors in my code (i am new to matlab):

function [ PR ] = PolyRegress( X, Y, d )
    O = ones(length(X), 1)
    N = [X O]

    for j = 2:d
        tmp = power(X, j)
        N = [tmp N]
    end

    %TO DO: compute PR


end
1
  • After looking at the output again I realized I was doing it properly all along. I did not see the 1.0e+05 * before the output :)
    – gprime
    Sep 25, 2011 at 19:42

2 Answers 2

4

It looks like the matlab function vander already does what you want to do.

1
  • Yes, this does do what I want. Just returns it in the reverse order. Thanks!
    – gprime
    Sep 25, 2011 at 19:43
2

The VANDER function will only generate powers of the vector upto d = length(X)-1. For a more general solution, you can use the BSXFUN function (works with any value of d):

N = bsxfun(@power, X(:), d:-1:0)

Example:

>> X = (1:.5:2);
>> d = 5;
>> N = bsxfun(@power, X(:), d:-1:0)
N =
            1            1            1            1            1            1
       7.5938       5.0625        3.375         2.25          1.5            1
           32           16            8            4            2            1

I'm not sure if this is the order you want, but it can be easily reversed: use 0:d instead of d:-1:0...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.