Got Questions? Get Answers.
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:
3D Matrix Optimization

Subject: 3D Matrix Optimization

From: Ryan

Date: 6 Jan, 2011 19:22:06

Message: 1 of 14

Hi all,

I am working on an optimization problem using two matrices of equal sizes, with each matrix having three dimensions (Matrix 1 and Matrix 2). I want to try to get to a "target number" (single number) by multiplying Matrix 1 by Matrix 2 and then aggregating to a single number changing only the one matrix (Matrix 1 or 2) with constraints on the changes of the individual element. The less difference between the new element and the old element, the better.

Is there anything in the Matlab Optimization Toolkit that can help us with this problem?

Thank you guys in advance.

Ryan

Subject: 3D Matrix Optimization

From: Ryan

Date: 7 Jan, 2011 14:40:20

Message: 2 of 14

Can anyone help me here?

Thanks

"Ryan " <rtang@axiomvaluation.com> wrote in message <ig54ou$9fd$1@fred.mathworks.com>...
> Hi all,
>
> I am working on an optimization problem using two matrices of equal sizes, with each matrix having three dimensions (Matrix 1 and Matrix 2). I want to try to get to a "target number" (single number) by multiplying Matrix 1 by Matrix 2 and then aggregating to a single number changing only the one matrix (Matrix 1 or 2) with constraints on the changes of the individual element. The less difference between the new element and the old element, the better.
>
> Is there anything in the Matlab Optimization Toolkit that can help us with this problem?
>
> Thank you guys in advance.
>
> Ryan

Subject: 3D Matrix Optimization

From: Steven_Lord

Date: 7 Jan, 2011 15:05:02

Message: 3 of 14



"Ryan " <rtang@axiomvaluation.com> wrote in message
news:ig78kk$ld3$1@fred.mathworks.com...
> Can anyone help me here?

I'm guessing many people here are like me -- I don't understand your
description of what you're trying to do.

Show a SMALL (3-by-3, maybe 4-by-4) worked-out example and explain what
you're trying to do in more depth and you may receive some suggestions.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: 3D Matrix Optimization

From: Mark Shore

Date: 7 Jan, 2011 15:40:06

Message: 4 of 14

When I was taught, the hierachy was scalar [1 x 1], vector [n x 1] or [1 x n], matrix [m x n], and higher dimensionality assemblages were called arrays (although you could refer to a matrix as a 2D array).

So, along with Steve, I don't understand what you are trying to do. And since you refer to a "3D matrix", I suspect you don't either.

Subject: 3D Matrix Optimization

From: Ryan

Date: 7 Jan, 2011 16:12:06

Message: 5 of 14

Here is the example:

Matrix 1: Matrix 2:

 

   3 4 5 0.5 0.7 0.9

   4 8 15 0.8 0.9 0.6

   9 6 4 1.2 0.3 0.2

 

When these matrices are multiplied and then summed, one gets an answer of 48.44. Let’s say the desired answer is 50, and we can only change values in Matrix 2 and want to minimize those changes. Is there anything in the Matlab Optimization Toolkit that can help us with this?

 

"Steven_Lord" <slord@mathworks.com> wrote in message <ig7a2u$qt3$1@fred.mathworks.com>...
>
>
> "Ryan " <rtang@axiomvaluation.com> wrote in message
> news:ig78kk$ld3$1@fred.mathworks.com...
> > Can anyone help me here?
>
> I'm guessing many people here are like me -- I don't understand your
> description of what you're trying to do.
>
> Show a SMALL (3-by-3, maybe 4-by-4) worked-out example and explain what
> you're trying to do in more depth and you may receive some suggestions.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlab.wikia.com/wiki/FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: 3D Matrix Optimization

From: Oleg Komarov

Date: 7 Jan, 2011 16:50:24

Message: 6 of 14

"Ryan " <rtang@axiomvaluation.com> wrote in message <ig7e0l$fsg$1@fred.mathworks.com>...
> Here is the example:
>
> Matrix 1: Matrix 2:
>
>
>
> 3 4 5 0.5 0.7 0.9
>
> 4 8 15 0.8 0.9 0.6
>
> 9 6 4 1.2 0.3 0.2
>
>
>
> When these matrices are multiplied and then summed, one gets an answer of 48.44. Let’s say the desired answer is 50, and we can only change values in Matrix 2 and want to minimize those changes. Is there anything in the Matlab Optimization Toolkit that can help us with this?


Please don't top post.
Where's the third dimensionality in those matrixes?

Oleg

Subject: 3D Matrix Optimization

From: Matt J

Date: 7 Jan, 2011 16:53:07

Message: 7 of 14

"Ryan " <rtang@axiomvaluation.com> wrote in message <ig7e0l$fsg$1@fred.mathworks.com>...
> Here is the example:
>
> Matrix 1: Matrix 2:
>
>
>
> 3 4 5 0.5 0.7 0.9
>
> 4 8 15 0.8 0.9 0.6
>
> 9 6 4 1.2 0.3 0.2
>
>
>
> When these matrices are multiplied and then summed, one gets an answer of 48.44.
=====================

Not me. I get an answer of 41.6

>> M1,M2

M1 =

     3 4 5
     4 8 15
     9 6 4


M2 =

    0.5000 0.7000 0.9000
    0.8000 0.9000 0.6000
    1.2000 0.3000 0.2000

>> dot(M1(:),M2(:))

ans =

   41.6000




 Let’s say the desired answer is 50, and we can only change values in Matrix 2 and want to minimize those changes. Is there anything in the Matlab Optimization Toolkit that can help us with this?
===============

It depends on what you mean by "minimize the changes". Do you mean to minimize the sum of squares of the changes, i.e.

min. dot(M2new(:)-M2(:))


If so, you could use quadprog, but it's overkill, since there is a simple analytical solution, which I've shown below. Essentially, the fact that Matrix1 and Matrix2 are matrices/arrays is irrelevant here. You can think of them as vectors M1(:) and M2(:). The problem you describe is equivalent to projecting M2(:) onto a plane.

c=50; %desired target value

M1=[ 3 4 5 ;... %Matrix 1
     4 8 15 ;...
     9 6 4],

M2=[ 0.5000 0.7000 0.9000;... %Matrix 2
    0.8000 0.9000 0.6000;...
    1.2000 0.3000 0.2000],

%reshape into vectors, for convenience
v=M1(:);
u0=M2(:);

u=u0+(c-dot(u0,v))/dot(v,v)*v; %project onto plane

M2new=reshape(u,size(M2)),

Check=dot(M2new(:),M1(:)),%=50



 

Subject: 3D Matrix Optimization

From: Matt J

Date: 7 Jan, 2011 17:00:22

Message: 8 of 14

"Matt J" wrote in message <ig7gdj$l24$1@fred.mathworks.com>...
>
> It depends on what you mean by "minimize the changes". Do you mean to minimize the sum of squares of the changes, i.e.
>
> min. dot(M2new(:)-M2(:))
======

Here, I really meant to write

   min. norm(M2new(:)-M2(:))

subject to

   dot(M2new(:), M1)=50



>
>
> If so, you could use quadprog, but it's overkill, since there is a simple analytical solution, which I've shown below. Essentially, the fact that Matrix1 and Matrix2 are matrices/arrays is irrelevant here. You can think of them as vectors M1(:) and M2(:). The problem you describe is equivalent to projecting M2(:) onto a plane.
>
> c=50; %desired target value
>
> M1=[ 3 4 5 ;... %Matrix 1
> 4 8 15 ;...
> 9 6 4],
>
> M2=[ 0.5000 0.7000 0.9000;... %Matrix 2
> 0.8000 0.9000 0.6000;...
> 1.2000 0.3000 0.2000],
>
> %reshape into vectors, for convenience
> v=M1(:);
> u0=M2(:);
>
> u=u0+(c-dot(u0,v))/dot(v,v)*v; %project onto plane
>
> M2new=reshape(u,size(M2)),
>
> Check=dot(M2new(:),M1(:)),%=50
>
>
>
>

Subject: 3D Matrix Optimization

From: Bruno Luong

Date: 7 Jan, 2011 18:52:06

Message: 9 of 14

% Data
M1 = rand(5)
M2 = rand(5)
pstarget = M1(:)'*M2(:)*1.1

% Engine
A = M1(:);
B = M2(:);
a2 = 1/(A'*A);
B0 = (pstarget*a2)*A;
dB = B-B0;
C = B0 + (dB-a2*A*A'*dB);
M2bis = reshape(C,size(M2))

% Check
sum(M2bis(:).*M1(:)) % equal to pstarget
norm(M2bis-M2,'fro')/norm(M2, 'fro') % M2bis is close to M2

Bruno

Subject: 3D Matrix Optimization

From: Ryan

Date: 7 Jan, 2011 20:05:05

Message: 10 of 14

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ig7ncm$p89$1@fred.mathworks.com>...
> % Data
> M1 = rand(5)
> M2 = rand(5)
> pstarget = M1(:)'*M2(:)*1.1
>
> % Engine
> A = M1(:);
> B = M2(:);
> a2 = 1/(A'*A);
> B0 = (pstarget*a2)*A;
> dB = B-B0;
> C = B0 + (dB-a2*A*A'*dB);
> M2bis = reshape(C,size(M2))
>
> % Check
> sum(M2bis(:).*M1(:)) % equal to pstarget
> norm(M2bis-M2,'fro')/norm(M2, 'fro') % M2bis is close to M2
>
> Bruno

Matt/Bruno/Steve,

Thanks for the answer. They are all perfect! One more follow up question here.

What if we impose one more constraint on the solution. As we vary the weights in Matrix 2, the sum of the columns in Matrix 2 cannot change. That is, column 1 must add to 2.5, column 2 must add to 1.9 and column 3 must add to 1.7. Any ideas?

 

We are working on an optimization problem using two matrices of equal size:

 

Matrix 1: Matrix 2:

 

   3 4 5 0.5 0.7 0.9

   4 8 15 0.8 0.9 0.6

   9 6 4 1.2 0.3 0.2

 

When these matrices are multiplied and then summed, one gets an answer of 48.44. Let’s say the desired answer is 50, and we can only change values in Matrix 2 and want to minimize those changes. Is there anything in the Matlab Optimization Toolkit that can help us with this?

Subject: 3D Matrix Optimization

From: Bruno Luong

Date: 7 Jan, 2011 20:19:05

Message: 11 of 14

"Ryan " <rtang@axiomvaluation.com> wrote in message <ig7rlh$6i6$1@fred.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message
>
> What if we impose one more constraint on the solution. As we vary the weights in Matrix 2, the sum of the columns in Matrix 2 cannot change. That is, column 1 must add to 2.5, column 2 must add to 1.9 and column 3 must add to 1.7. Any ideas?

Just writing down the equations. They are all linear so it can be solved with matrix manipulation only.

Bruno

Subject: 3D Matrix Optimization

From: Matt J

Date: 7 Jan, 2011 23:12:05

Message: 12 of 14

"Ryan " <rtang@axiomvaluation.com> wrote in message <ig7rlh$6i6$1@fred.mathworks.com>...
>
>
> What if we impose one more constraint on the solution. As we vary the weights in Matrix 2, the sum of the columns in Matrix 2 cannot change. That is, column 1 must add to 2.5, column 2 must add to 1.9 and column 3 must add to 1.7. Any ideas?
==============

Note that there are cases where this will have no solution. For example if

Matrix1=ones(N); %N=length(Matrix2)

then you would equivalently be trying to constrain not only the sums of the individual columns, but the sum of those sums. These cannot be independently constrained.
 

Subject: 3D Matrix Optimization

From: Matt J

Date: 7 Jan, 2011 23:25:21

Message: 13 of 14

"Matt J" wrote in message <ig86k5$19a$1@fred.mathworks.com>...
>
> Note that there are cases where this will have no solution. For example if
>
> Matrix1=ones(N); %N=length(Matrix2)
>
> then you would equivalently be trying to constrain not only the sums of the individual columns, but the sum of those sums. These cannot be independently constrained.
================

If a solution does exist, however, here is the modification to my previous solution that will find it,


M1=[ 3 4 5 ;...
     4 8 15 ;...
     9 6 4],

M2=[ 0.5000 0.7000 0.9000;...
    0.8000 0.9000 0.6000;...
    1.2000 0.3000 0.2000],

c=[sum(M2),50]'; %target vales

V=[kron(eye(3),[1;1;1]) M1(:)];
u0=M2(:);


u=u0+ V*((V.'*V)\(c-V'*u0));

M2new=reshape(u,size(M2)),

Subject: 3D Matrix Optimization

From: Matt J

Date: 9 Jan, 2011 05:07:04

Message: 14 of 14

"Matt J" wrote in message <ig87d1$k4d$1@fred.mathworks.com>...
>
>
> u=u0+ V*((V.'*V)\(c-V'*u0));
========


It turns out that this formula can be simplified and the computations streamlined (see below) by analytically computing inv(V.'*V). Interestingly, even though the column sums of Matrix 2 are to be constrained, one never actually has to explicitly compute them, only those of Matrix 1





%%data
M1=[ 3 4 5 ;...
     4 8 15 ;...
     9 6 4],

M2=[ 0.5000 0.7000 0.9000;...
        0.8000 0.9000 0.6000;...
        1.2000 0.3000 0.2000],

    

targetvalue=50;

%%engine
columnsums1=sum(M1,1);
n=size(M2,1);
d=1/(trace(M1.'*M1)-norm(columnsums1)^2/n);
K=targetvalue - trace(M1.'*M2);

M2new=M2+K*d*(bsxfun(@minus,M1,columnsums1/n)),

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