How to plot bar graph for two different values?

2 visualizaciones (últimos 30 días)
Prashant Bhagat
Prashant Bhagat el 2 de Sept. de 2022
Respondida: Yogesh el 5 de Sept. de 2022
I am trying to run the below code for two different values or multiple values of A and I also want to run the same script for 100 times and then want to plot the same results in the bargraph. can anyone help? Thanks in advance.
close all;
clear;
clc;
% Defining variables
A = [10 20]; % Total number of transmitters
B = cell(1,1,6);
Percentage_of_collission = cell(1,1,6);
p = 0.0001;
Length_of_packet = 1000; % Maximum packet length in bits
Bitrate = 100; % bit rate for each transmitter in bits/sec
Trasmission_Time = Length_of_packet/Bitrate;
Total_Time = 1000000; % Total time for simulation in seconds
for jj = 1:length(A)
for k = 1:A
skipping = 0;
for i = 1:(Total_Time-10)
if skipping > 0
User(k,i) = 1;
skipping = skipping - 1;
else
probability_of_transmission = rand(1);
if probability_of_transmission <= p
User(k,i) = 1;
skipping = 9;
else
User(k,i) = 0;
end
end
end
end
X = sum(User(User == 1));
Total_no_of_slots_transmitted = X;
Number_of_overlapping_packets_per_column = sum(User);
%Creating row pairs to compare
row_pairs = nchoosek(1:size(User,1),2);
%disp(row_pairs);
number_of_pairs = size(row_pairs,1);
number_of_overlaps = zeros(number_of_pairs,1);
%Finding overlapping and counting them
slot_overlapping = zeros(1,11);
maxslot = -inf;
for ii = 1:number_of_pairs
number_of_overlaps(ii,1) = nnz(User(row_pairs(ii,1),:) & User(row_pairs(ii,2),:));
slot = number_of_overlaps(ii,1);
maxslot = max(maxslot, slot);
full10 = floor(slot/10);
leftover = mod(slot,10);
slot_overlapping(11) = slot_overlapping(11) + full10;
if slot == 0 || leftover ~= 0
slot_overlapping(leftover+1) = slot_overlapping(leftover+1) + 1;
end
end
x = (0:10);
end
bar(x, slot_overlapping);

Respuestas (1)

Yogesh
Yogesh el 5 de Sept. de 2022
To solve this issue, I have aggregated results and plotted them, the changes made to the code are viz. Introduction of the results variable, and for better depiction I have used a bar3 function.
for more info on bar 3 function utilise this link
% Defining variables
A = [10 20]; % Total number of transmitters
B = cell(1,1,6);
Percentage_of_collission = cell(1,1,6);
p = 0.0001;
Length_of_packet = 1000; % Maximum packet length in bits
Bitrate = 100; % bit rate for each transmitter in bits/sec
Trasmission_Time = Length_of_packet/Bitrate;
Total_Time = 1000000; % Total time for simulation in seconds
results = [];
for jj = 1:length(A)
for k = 1:A
skipping = 0;
for i = 1:(Total_Time-10)
if skipping > 0
User(k,i) = 1;
skipping = skipping - 1;
else
probability_of_transmission = rand(1);
if probability_of_transmission <= p
User(k,i) = 1;
skipping = 9;
else
User(k,i) = 0;
end
end
end
end
X = sum(User(User == 1));
Total_no_of_slots_transmitted = X;
Number_of_overlapping_packets_per_column = sum(User);
%Creating row pairs to compare
row_pairs = nchoosek(1:size(User,1),2);
%disp(row_pairs);
number_of_pairs = size(row_pairs,1);
number_of_overlaps = zeros(number_of_pairs,1);
%Finding overlapping and counting them
slot_overlapping = zeros(1,11);
maxslot = -inf;
for ii = 1:number_of_pairs
number_of_overlaps(ii,1) = nnz(User(row_pairs(ii,1),:) & User(row_pairs(ii,2),:));
slot = number_of_overlaps(ii,1);
maxslot = max(maxslot, slot);
full10 = floor(slot/10);
leftover = mod(slot,10);
slot_overlapping(11) = slot_overlapping(11) + full10;
if slot == 0 || leftover ~= 0
slot_overlapping(leftover+1) = slot_overlapping(leftover+1) + 1;
end
end
x = (0:10);
results = [results;slot_overlapping];
% bar(x+jj, slot_overlapping);
end
results
results = 2×11
43 0 0 0 0 1 0 0 1 0 0 35 1 0 2 3 1 0 1 1 1 0
bar3(results);

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by