Combine two different matrices to create one new matrix of all matrix combinations

조회 수: 1 (최근 30일)
Hi, I have the following matrices
WT_energy_supply(:,:,M) and PV_power_output(:,:,K)
which are 365 x 24 matrices and M = 3 and K = 10. Therefore there are 30 possible combinations of WY_energy_supply and PV_power_output. I am looking to create a set of matrices (in the form (:,:,Z)) that add corresponding elements in each matrix to form this new set (again 365x24 with 30 different matrices) Does anyone have any idea where to start coding this?
Thank you

채택된 답변

Sean de Wolski
Sean de Wolski 2012년 5월 11일
Nice brain teaser!
%Sample data:
A = ones(4,4,5);
B = bsxfun(@times,ones(4,4,3),cat(3,1,2,3));
%Nice to know:
sz = size(B);
%Engine:
C = reshape(bsxfun(@plus,A, reshape(B,sz(1),sz(2),1,sz(3))),sz(1),sz(2),[]);
  댓글 수: 18
Sean de Wolski
Sean de Wolski 2012년 5월 11일
@Andrei, permute is much slower than reshape!
Andrew Alkiviades
Andrew Alkiviades 2012년 5월 12일
@Sean
Still a bit confused on how to code "C" to take the form
C(:,:,Z) = ...

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Andrei Bobrov
Andrei Bobrov 2012년 5월 11일
eg:
M = 3;
K = 10;
WT_energy_supply = randi(10,10,4,M);
PV_power_output = randi([10 50],10,4,K);
%solution
ij = fullfact([M K]);
out = WT_energy_supply(:,:,ij(:,1)) + PV_power_output(:,:,ij(:,2)) ;
  댓글 수: 5
Andrew Alkiviades
Andrew Alkiviades 2012년 5월 11일
having tried this code i get the following error "Array dimensions must match for binary array op."
Do I need to define M and K as 3 and 10 since I have them previously in the code for working out WT_energy_supply and PV_power_output?
This may be a stupid question but why can't I just add WT_energy_supply(:,:,M) to PV_power_output(:,:,K) since this is really what I am trying to do?
Thank you
Sean de Wolski
Sean de Wolski 2012년 5월 11일
That's what bsxfun does for you!
think about it, how can you add two things that are different sizes?

댓글을 달려면 로그인하십시오.


owr
owr 2012년 5월 11일
To generate all combinations of K and M, check out the file exchange function "allcomb": href=""<http://www.mathworks.com/matlabcentral/fileexchange/10064</a>>
Ive been using it for years, works with more than 2 parameters as well.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by