필터 지우기
필터 지우기

Formulation to Matlab code

조회 수: 3 (최근 30일)
Maroco Sc
Maroco Sc 2019년 5월 14일
답변: Steven Lord 2019년 5월 14일
How to write this formulation in Matlab:
2.JPG
is it :
for i=1:N
S(i) = -PT(i) * log(PT(i));
end
  댓글 수: 2
Dimitris Kalogiros
Dimitris Kalogiros 2019년 5월 14일
What is the base of the logarithm? Is it 10 ? If so, then you must use log10() .
Maroco Sc
Maroco Sc 2019년 5월 14일
I have checked several papers, they did not mention the base of logarithm. if it is 10, then the code will be:
for i=1:N
S(i) = -PT(i) * log10(PT(i));
end
Right?

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

채택된 답변

Steven Lord
Steven Lord 2019년 5월 14일
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

추가 답변 (1개)

Raj
Raj 2019년 5월 14일
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

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by