Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
vector and array element multiplication

Subject: vector and array element multiplication

From: Gorbeña

Date: 19 Apr, 2013 01:26:09

Message: 1 of 7

Hi,
I would like to create a second order polynomial from a vector or a matrix. Each element of the vector or matrix corresponds to an independedent variable.

Given vector A, where A = [x1 x2 x3], I want the following result:

[x1 x1² x1*x2 x1*x3 x2 x2*x1 x2² x1*x3 x3 x3*x1 x3*x2 x3²]

In case I have matrix B = [a1 a2 a3; b1 b2 b3], the desired result will be:

[a1 a1² a1*a2 a1*a3 a2 a2*a1 a2² a1*a3 a3 a3*a1 a3*a2 a3²;
 b1 b1² b1*b2 b1*b3 b2 b2*b1 b2² b1*b3 b3 b3*b1 b3*b2 b3²]

Does anybody knows how to do this?

Thanks

Subject: vector and array element multiplication

From: Nasser M. Abbasi

Date: 19 Apr, 2013 02:29:46

Message: 2 of 7

On 4/18/2013 8:26 PM, Gorbeña wrote:
> Hi,
> I would like to create a second order polynomial from a vector or a matrix. Each element
>of the vector or matrix corresponds to an independedent variable.
>
> Given vector A, where A = [x1 x2 x3], I want the following result:
>
> [x1 x1² x1*x2 x1*x3 x2 x2*x1 x2² x1*x3 x3 x3*x1 x3*x2 x3²]
>
> In case I have matrix B = [a1 a2 a3; b1 b2 b3], the desired result will be:
>
> [a1 a1² a1*a2 a1*a3 a2 a2*a1 a2² a1*a3 a3 a3*a1 a3*a2 a3²;
> b1 b1² b1*b2 b1*b3 b2 b2*b1 b2² b1*b3 b3 b3*b1 b3*b2 b3²]
>
> Does anybody knows how to do this?
>
> Thanks
>

You can make each poly by multiplying each row by itself but after
transposing. (i.e. do an outer product)

-----------------
syms x1 x2 x3
V = [x1 x2 x3] %one row
C = V.'*V;
C = [V C(:).']
---------------------
  
[ x1, x2, x3, x1^2, x1*x2, x1*x3, x1*x2, x2^2, x2*x3, x1*x3, x2*x3, x3^2]


So for a matrix, just do the above outer product for its of its rows.

--Nasser

Subject: vector and array element multiplication

From: Bruno Luong

Date: 19 Apr, 2013 06:40:19

Message: 3 of 7

"Gorbeña " <eduardogg@oceanica.ufrj.br> wrote in message <kkq6fh$1um$1@newscl01ah.mathworks.com>...
> Hi,
> I would like to create a second order polynomial from a vector or a matrix. Each element of the vector or matrix corresponds to an independedent variable.
>
> Given vector A, where A = [x1 x2 x3], I want the following result:
>
> [x1 x1² x1*x2 x1*x3 x2 x2*x1 x2² x1*x3 x3 x3*x1 x3*x2 x3²]
>

If really a 2nd order polynomial is involved then:
1. You miss constant term (1) = x1^0*x2^0*x3^0.
2. x1*x2 is dependent to x2*x1, etc... they are counted twice

Bruno

Subject: vector and array element multiplication

From: Gorbeña

Date: 19 Apr, 2013 12:43:09

Message: 4 of 7

Bruno, you are right about the constant term (1) = 1^0*x2^0*x3^0. It is misssing.
About counting twice, I wrote the extended form:

C1*(x1*x2) + C2*(x2*x1) = C3*(x1*x2) = C3*(x2*x1)

Anyways, do you have any ideas how to do it?

Kind regards,

Eduardo

Subject: vector and array element multiplication

From: Gorbeña

Date: 19 Apr, 2013 13:50:09

Message: 5 of 7

Thank you Nasser! Works perfectly!!

Subject: vector and array element multiplication

From: Steven_Lord

Date: 19 Apr, 2013 15:10:37

Message: 6 of 7



"Gorbea " <eduardogg@oceanica.ufrj.br> wrote in message
news:kkre4t$e73$1@newscl01ah.mathworks.com...
> Bruno, you are right about the constant term (1) = 1^0*x2^0*x3^0. It is
> misssing.
> About counting twice, I wrote the extended form:
> C1*(x1*x2) + C2*(x2*x1) = C3*(x1*x2) = C3*(x2*x1)
>
> Anyways, do you have any ideas how to do it?

Assuming you have Symbolic Math Toolbox this won't necessarily get you the
terms in your desired order, but:

syms x1 x2 x3
A = [1; x1; x2; x3]*[1, x1, x2, x3];
unique(A).'

Alternately, if you're doing this to do some sort of experimental design,
take a look at X2FX from Statistics Toolbox. This will give you control over
the exact ordering of the terms, though it only works with double (and
perhaps single, I haven't tried) data not symbolic objects.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: vector and array element multiplication

From: Bruno Luong

Date: 19 Apr, 2013 22:47:08

Message: 7 of 7

"Gorbeña " <eduardogg@oceanica.ufrj.br> wrote in message <kkre4t$e73$1@newscl01ah.mathworks.com>...
> Bruno, you are right about the constant term (1) = 1^0*x2^0*x3^0. It is misssing.
> About counting twice, I wrote the extended form:
>
> C1*(x1*x2) + C2*(x2*x1) = C3*(x1*x2) = C3*(x2*x1)
>
> Anyways, do you have any ideas how to do it?
>

If you prefer non duplicated basis at arbitrary order, here is the code using my FEX
http://www.mathworks.fr/matlabcentral/fileexchange/17818

x = [2 3 5 7 11;
      1 2 3 5 7];

n = size(x,2);
order = 2; % 2 mean 2nd order polynomials
p = allVL1(n,order ,'<='); % FEX,
p = reshape(p', 1, n, []);
A = bsxfun(@power, x, p);
A = squeeze(prod(A,2))

% Bruno

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us