Formulation to Matlab code
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Maroco Sc
am 14 Mai 2019
Beantwortet: Steven Lord
am 14 Mai 2019
How to write this formulation in Matlab:
is it :
for i=1:N
S(i) = -PT(i) * log(PT(i));
end
2 Kommentare
Dimitris Kalogiros
am 14 Mai 2019
What is the base of the logarithm? Is it 10 ? If so, then you must use log10() .
Akzeptierte Antwort
Steven Lord
am 14 Mai 2019
There's no need to loop. The log and log10 functions can operate on arrays of data, and as long as you use element-wise multiplication you can do this in one line. [Actually, I'll need two; one to create sample data and one for the actual operation.]
PT = 10*rand(10);
S = -PT.*log10(PT);
You can compare this with the result of doing the same operation one element at a time to see if they're different.
S2 = zeros(size(PT));
for whichelement = 1:numel(PT)
S2(whichelement) = -PT(whichelement).*log10(PT(whichelement));
end
S-S2
0 Kommentare
Weitere Antworten (1)
Raj
am 14 Mai 2019
Since your equation asks for 'log' not 'ln' , I think the code should be:
for i=1:N
S(i) = -PT(i) * log10(PT(i));
end
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!