How to calculate the hit ratio?
조회 수: 3 (최근 30일)
이전 댓글 표시
Good day everyone,
Based on the below screenshot I want to calculate the hit ratio. The formula for calculating the value would be: cachehits+cachemisses/cachehits.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1088795/image.png)
For 1st second I get two types: Cachehits and Cachemisses. In this way I have the data for all the nodes in the network that I simulated. I want to know is there anyway in matlab that I can give the formula once and I can get the values for all my rows?
Please let me know. Thank you.
댓글 수: 0
답변 (1개)
sudobash
2022년 8월 5일
Hello!
As per my understanding, the Hit Ratio for the entire network needs to be computed. Here is a solution:
%% Example Input
inputTable = table();
inputTable.Time = [1;1;2;2;3;3;4;4];
inputTable.Node = ["R7";"R7";"R7";"R7";"R7";"R7";"R7";"R7"];
inputTable.Type = ["CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses"];
inputTable.Packets = [1;22;4;15;9;15;9;13];
inputTable
%% Solution
% Find the total number of cache hits
totalCacheHits = sum(inputTable(inputTable.Type == "CacheHits",:).Packets)
% Find the total number of cache misses
totalCacheMisses = sum(inputTable(inputTable.Type == "CacheMisses",:).Packets)
% Compute the Hit Ratio using the formula
hitRatio = (totalCacheMisses + totalCacheHits)/totalCacheHits
You can also find the hit ratio for each node by selecting only specific nodes like this:
R7CacheMisses = sum(inputTable(inputTable.Type == "CacheMisses" & inputTable.Node == "R7",:).Packets)
Hope this addresses your question.
댓글 수: 4
sudobash
2022년 8월 8일
Hi!
As per my understanding, I would suggest you reformat your table and find hit ratio in this way :
%% Example Input
inputTable = table();
inputTable.Time = [1;2;3;4];
inputTable.Node = ["R7";"R7";"R7";"R7"];
inputTable.Hit = [1;4;9;9]; % Hit is the number of packets of the type "CacheHit"
inputTable.Miss = [22;15;15;13]; % Miss is the number of packets of the type "CacheMiss"
inputTable
inputTable.HitRatio = (inputTable.Hit + inputTable.Miss)./inputTable.Hit
If you wish not to reformat, then you could use the following workaround:
%% Example Input
inputTable = table();
inputTable.Time = [1;1;2;2;3;3;4;4];
inputTable.Node = ["R7";"R7";"R7";"R7";"R7";"R7";"R7";"R7"];
inputTable.Type = ["CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses";"CacheHits";"CacheMisses"];
inputTable.Packets = [1;22;4;15;9;15;9;13];
inputTable;
for i = 1:2:height(inputTable)
inputTable.HitRatio(i) = (inputTable.Packets(i)+inputTable.Packets(i+1))/inputTable.Packets(i);
inputTable.HitRatio(i+1) = (inputTable.Packets(i)+inputTable.Packets(i+1))/inputTable.Packets(i);
end
inputTable
Hope this solves your question.
참고 항목
카테고리
Help Center 및 File Exchange에서 Communications Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!