Multiply each element of matrix with corresponding element of another matrix by not using loop. Is there any other more efficient way?

조회 수: 29 (최근 30일)
Hi everyone!
I have a simple and yet useful question, whose answer could reduce my amount of code (make it more efficient).
I have two matrices and each is of dimensions 1x31. The first matrix called "Prod_year" represents total yearly electricity production and the second one called "fundamental_price_level" is a price curve with future projected electricity prices. I need to multiply each corresponding element of "Prod_year" (x,y) with the correspinding element of "fundamental_price_level" (x,y) or price_floor (this is just one value) when needed. The resulting matices "Mer_EXP_rev", "SP_EXP_rev" and "CfD_EXP_rev" are three different revenue streams, assuming different assumptions about the payment methods to the power plant. These matrices are also all of dimensions 1x31.
Instead of using for loops I would like to economise and use something simpler. Therefore, is there a way to make my code more efficient?
FYI: the first code I am posting works
Price_floor = 45;
%Merchant EXP revenues
Mer_EXP_rev = zeros(1,31);
for x=1
for y=1:31
Mer_EXP_rev (x,y) = Prod_Year (x,y) * fundamental_price_level (x,y);
end
end
%SP EXP revenu"#Cfes
SP_EXP_rev =zeros(1,31);
for x=1
for y=1:31
if fundamental_price_level(x,y) < Price_floor
SP_EXP_rev(x,y) = Price_floor * Prod_Year (x,y);
elseif fundamental_price_level(x,y) > Price_floor
SP_EXP_rev(x,y) = fundamental_price_level(x,y) * Prod_Year (x,y);
else fundamental_price_level(x,y) = Price_floor;
SP_EXP_rev(x,y) = Price_floor * Prod_Year (x,y);
end
end
end
%CfD EXP revenues
CfD_EXP_rev = zeros(1,31);
for x=1
for y=1:31
CfD_EXP_rev (x,y) = Price_floor * Prod_Year (x,y);
end
end
I tried doing the below code for the case of "Mer_EXP_rev" but it did not work because I get an error saying "Error using * Inner matrix dimensions must agree."
Mer_EXP_Rev2 = zeros(1,31);
Mer_EXP_Rev2(1,1:31) = Prod_Year(1,1:31) * fundamental_price_level (1,1:31);
I would appreciate yout help to learn how to handle matrix multiplication in a better way.
Thanks a lot for your time!
Mak

채택된 답변

KSSV
KSSV 2021년 11월 16일
  댓글 수: 4
Mak Dukan
Mak Dukan 2021년 11월 16일
How would you apply it to this code?
SP_EXP_rev =zeros(1,31);
for x=1
for y=1:31
if fundamental_price_level(x,y) < Price_floor
SP_EXP_rev(x,y) = Price_floor * Prod_Year (x,y);
elseif fundamental_price_level(x,y) > Price_floor
SP_EXP_rev(x,y) = fundamental_price_level(x,y) * Prod_Year (x,y);
else fundamental_price_level(x,y) = Price_floor;
SP_EXP_rev(x,y) = Price_floor * Prod_Year (x,y);
end
end
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by