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!