polyFeatures.m 727 B

12345678910111213141516171819202122232425262728
  1. function [X_poly] = polyFeatures(X, p)
  2. %POLYFEATURES Maps X (1D vector) into the p-th power
  3. % [X_poly] = POLYFEATURES(X, p) takes a data matrix X (size m x 1) and
  4. % maps each example into its polynomial features where
  5. % X_poly(i, :) = [X(i) X(i).^2 X(i).^3 ... X(i).^p];
  6. %
  7. % You need to return the following variables correctly.
  8. X_poly = zeros(numel(X), p);
  9. % ====================== YOUR CODE HERE ======================
  10. % Instructions: Given a vector X, return a matrix X_poly where the p-th
  11. % column of X contains the values of X to the p-th power.
  12. %
  13. %
  14. z = X;
  15. for i = 1:p
  16. X_poly(:, i) = z;
  17. z = z .* X;
  18. end
  19. % =========================================================================
  20. end