Expand the matrix with polynomials

Say I have a matrix A with 3 columns c1 , c2 and c3 .

 1 2 9 3 0 7 3 1 4 

And I want a new dimension matrix (3x3n) in which the first column is c1 , the second column is c1^2 , n is the column c1^n , column n + 1 is c2 , column n + 2 is equal to c2^2 , etc. Is there a quick way to do this in MATLAB?

+4
source share
3 answers

Combining PERMUTE , BSXFUN , and RESHAPE , you can do this quite easily so that it works for any size A I have separated the instructions for clarity, you can combine them on one line if you want.

 n = 2; A = [1 2 9; 3 0 7; 3 1 4]; [r,c] = size(A); %# reshape A into a r-by-1-by-c array A = permute(A,[1 3 2]); %# create a r-by-n-by-c array with the powers A = bsxfun(@power,A,1:n); %# reshape such that we get a r-by-n*c array A = reshape(A,r,[]) A = 1 1 2 4 9 81 3 9 0 0 7 49 3 9 1 1 4 16 
+3
source

Try the following (I don’t have access to Matlab now), it should work

  A = [1 2 9;  3 0 7;  3 1 4];
     B = [];
     for i = 1: n
          B = [B A. ^ i];
     end
     B = [B (:, 1: 3: end) B (:, 2: 3: end) B (:, 3: 3: end)];
    

Additional RAM:

  A = [1 2 9;  3 0 7;  3 1 4];
     B = zeros (3.3 * n);
     for i = 1: n
          B (3 * (i-1) +1: 3 * (i-1) +3, :) = A. ^ i;
     end
     B = [B (:, 1: 3: end) B (:, 2: 3: end) B (:, 3: 3: end)];
    
+1
source

Here is one solution:

 n = 4; A = [1 2 9; 3 0 7; 3 1 4]; Soln = [repmat(A(:, 1), 1, n).^(repmat(1:n, 3, 1)), ... repmat(A(:, 2), 1, n).^(repmat(1:n, 3, 1)), ... repmat(A(:, 3), 1, n).^(repmat(1:n, 3, 1))]; 
0
source

Source: https://habr.com/ru/post/1438213/


All Articles