Search code examples
matlabmatrixelementbsxfun

Create third matrix in MATLAB from combination of two other matrices


I have two expressions in MATLAB that represent a 365x24 matrix. The first expression has 10, 365x24 matrices and is therefore

PV_power_output(:,:,K) 

and the second expression which is again 365x24 but with three possible matrices therefore is

WT_energy_supply(:,:,M);ode here

Now, I am looking to create a third matrix that adds the elements in the same position above and thus form a 365x24 matrix. However I want a set of matrix with all possible combinations of the two expressions shown above (therefore this matrix must be 365x24x30.

How do I go about this? What about the bsxfun function in MATLAB?


Solution

  • Expand the original matrices (which for clarity I name a and b) with repmat and then just add them, bsxfun is not needed.

    repmat(a,[1 1 size(b,3)]) + repmat(b,[1 1 size(a,3)]))
    

    Update

    >> size(a)
    ans = 
        364  24  10
    
    >> size(b)
    ans = 
        364  24  3
    
    >> c=repmat(a,[1 1 size(b,3)])+repmat(b,[1 1 size(a,3)]);
    >> size(c)
    ans = 
        364  24  30
    

    It looks fine to me. Of course you'll have to replace my variables a and b with your variables PV_power_output and WT_energy_supply.