Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:22:03+00:00 2026-06-12T16:22:03+00:00

Say I have a matrix with 3 columns: c1 , c2 , c3 ,

  • 0

Say I have a matrix with 3 columns: c1, c2,c3, and I want to create a new matrix in which each column is any possible product of two of the columns of this matrix.

So, if I had a matrix with d columns, I would like to create a new matrix with d+d(d-1)/2+d columns. For example, consider the matrix with 3 columns c1, c2,c3. The matrix that I would like to create should have the columns c1, c2,c3, c1xc2, c2xc3,c1xc3, c1^2, c2^2 and c3^2.

Is there any efficient way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:22:05+00:00Added an answer on June 12, 2026 at 4:22 pm

    I’m embarrassed to post this – I’m sure there must be a simpler way (there is a MUCH simpler way – see my december update at the bottom of the answer), but this will do the job:

    A = [1 2 3; 4 5 6];
    n =  size(A, 2);
    
    B = A(:, reshape(ones(n, 1) * (1:n), 1, n^2)) .* repmat(A, 1, n);
    Soln = [A, B(:, logical(reshape(tril(toeplitz(ones(n, 1))), 1, n^2)'))];
    

    The calculation is not efficient, since in the B step I actually calculate double the number of combinations that I need (ie I get c1.*c1, c1.*c2, c1.*c3, c2.*c1, c2.*c2, c2.*c3, c3.*c1, c3.*c2, c3.*c3), and then in the second step I pull out only the columns that I need (eg I get rid of c3.*c1 as I’ve already got c1.*c3 and so on).

    UPDATE: Was just out driving and a much better method occurred to me. You just need to construct two index vectors of the form: I1 = [1 1 1 2 2 3] and I2 = [1 2 3 2 3 3], then (A(:, I1) .* A(:, I2)) will get you all the column products you are after. I’m away from my computer at the moment, but will come back later and work out a general way to construct the index vectors. I think it can be fairly easily accompished using the tril(toeplitz) construction. Cheers. Will update in a few hours.

    UPDATE: Rody’s second solution (+1) is exactly what I had in mind with my previous update so I won’t bother repeating what he has done there now. Yoda’s is quite neat too actually, so another +1.

    DECEMBER UPDATE: Funnily enough, after working on it here, I had to revisit this problem for my own research (coding up White’s test for heteroscedasticity). I’m actually favoring a new approach now, recommended (somewhat cryptically) by @slayton in the comments. Specifically, using nchoosek. My new solution looks like this:

    T = 20; K = 4;
    X = randi(100, T, K);
    Index = nchoosek((1:K), 2);
    XAll = [X, X(:, Index(:, 1)) .* X(:, Index(:, 2)), X.^2];
    

    nchoosek yields exactly the indices we need to construct the cross-products quickly and easily!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a R object say: mat <- matrix(1:100,nrow=20) I want to send this
Say I have a matrix ( MxN ) which has its rows and columns
Let's say I have a matrix x which contains 10 rows and 2 columns.
I have a 2D matrix, say M=zeros(10,10); I have another column matrix, V=[1;2;3;4;5;6;5;4;3;2]; I
Say in Matlab I have a matrix like this: 1 2 3 4 5
Say I have a matrix A with 3 columns c1 , c2 and c3
I have two 50 x 6 matrices, say A and B . I want
Say I have two matrix, A and B: mth <- c(rep(1:5,2)) day <- c(rep(10,5),rep(11,5))
say I want to multiply two matrices together, 50 by 50. I have 2
As a simple example, let's say you have this matrix: M = [omega 1;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.