1

Is there a quick and easy method of taking some vector [x,y,z] and generating [xx,xy,xz,yy,yz,zz] ?

nchoosek is going to omit the squared terms. I can manually adding them... Just wondering if there's a better solution

2c2c
  • 4,694
  • 7
  • 22
  • 33
  • 1
    What is `xx`? Are you not missing a `*` sign here? Also, what are `x`, `y`, `z`? Scalars or matrices? That's important to consider because multiplication may not be commutative. – jub0bs May 01 '16 at 07:55

1 Answers1

2

Assuming that the vector consists only of single elements, one way I can suggest is to use a combination of find and triu. Given that your vector is stored in the variable A, try:

[X,Y] = find(triu(ones(numel(A))));
P = A(X).*A(Y);

triu extracts out the upper triangular portion of the matrix. By extracting out the upper triangular portion of a matrix entirely of ones, we can use find to determine the row and column locations of the non-zero entries. Each row and column location corresponds to a pair of locations in your vector that you would multiply together to achieve your desired result. The row and column locations would be stored in X and Y respectively which you would then use to access the vector A and element-wise multiply to achieve the desired result.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • You answered, but what if `x,y,z` were each column vectors themselves? – 2c2c May 01 '16 at 07:58
  • @2c2c I assumed in your post that the elements `x` `y` and `z` were all single elements. If you desire that these are column vectors instead, have a look at this canonical post: http://stackoverflow.com/questions/21895335/generate-a-matrix-containing-all-combinations-of-elements-taken-from-n-vectors/21895344#21895344 – rayryeng May 01 '16 at 08:01