Multiply each element of matrix with corresponding element of another matrix by not using loop. Is there any other more efficient way?
31 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mak Dukan
am 16 Nov. 2021
Kommentiert: Mak Dukan
am 16 Nov. 2021
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
0 Kommentare
Akzeptierte Antwort
KSSV
am 16 Nov. 2021
Mer_EXP_rev = Prod_Year.*fundamental_price_level;
Read about element by element operations.
4 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!