Problem with for loop in given algorithm

Hello all, I am trying to code the following statement using for loop but not getting it clearly.
Queue length at a node in time slot 't+1' = Queue length at a node in time slot 't' + number of packets arrival in time slot 't'.
This is what I had tried:
T = 100; % Total slots
nodes = 8 % Total nodes
ini_q_len = 10; % Queue length at node 1 in time slot 't'
n = 10; % number of packets
p = 0.2; % probability of successful arrival of packets
for t = 1:T % each time slot
%% Queue length of each nodes
for i = 1:nodes
X(i) = ini_q_len+ binornd(n,p);
end
end
The problem with this is that I am not getting how to bring in for loop Queue length at a node in time slot 't' i.e., how to include previous slot queue length.
Any help in this regard will be highly appreciated.

回答(1 个)

unless Queue length value is not varying, you can initialize it as below and access in 2D matrix to include previous queue length values
T = 100; % Total slots
nodes = 8 % Total nodes
ini_q_len = repmat(10,1,100); % Queue length at node 1 in time slot 't'
K = 4; % some constant
for t = 1:T % each time slot
%% Queue length of each nodes
for i = 1:nodes
X(i,t) = ini_q_len(t)+ K;
end
end

13 个评论

Thank you sir for your answer. I had edited my question Could pls check it once again....
@VBBV sir in your case also every time ini_q_len = 10 is coming in for loop....Its not what I am expecting ...
May be like this
T = 100; % Total slots
nodes = 8 % Total nodes
ini_q_len = zeros(1,100); % Queue length at node 1 in time slot 't'
% assume 10 initially
ini_q_len(1) = 10;
K = 4; % some constant
for t = 1:T-1 % each time slot
%% Queue length of each nodes
for i = 1:nodes
X(i,t) = ini_q_len(t)+ K;
end
% generate values for ini_q_len using an expression (shown below is
% e.g)
ini_q_len(t+1) = ini_q_len(t)+num_packets(t);
end
Thank you sir...I had tried like this , could pls check whether its correct
clc;
clear all;
close all;
T = 100; % Total slots
nodes = 8; % Total nodes
ini_q_len = repmat(10,T,nodes); % initial Queue length at every node in time slot 't'
K = 4; % some constant
n = 10; % number of packets
p = 0.2; % probability of successful arrival of packets
for t = 1:T % each time slot
t
%% Queue length of each nodes
for i = 1:nodes
i
b = binornd(n,p);
X(t,i) = ini_q_len(t,i)+ b;
end
end
Queue length at a node in time slot 't+1' = Queue length at a node in time slot 't' + number of packets arrival in time slot 't'.
This is not whats happening in your code. Check code i have shown how its done
@VBBV sir in your code, num_packets(t) is not declared earlier...from where its coming ?
It's an e.g. to include packets in your algorithm. You can replace it with suitable variable. E g. Variable n in your case which is assigned 10 earlier.
If the number of packets for each time slot is same , then you can modify the line in outer loop as
ini_q_len(t+1) = ini_q_len(t)+n;
otherwise
% where n is a vector containing number of packets for each time slot
ini_q_len(t+1) = ini_q_len(t) + n(t);
@VBBV sir, Still not coming properly ....
what is error you get ? can you explain Still not coming properly .... ? you need to use this code
T = 100; % Total slots
nodes = 8 % Total nodes
ini_q_len = zeros(1,100); % Queue length at node 1 in time slot 't'
n = 10;
% assume 10 initially
ini_q_len(1) = 10;
K = 4; % some constant
for t = 1:T-1 % each time slot
%% Queue length of each nodes
for i = 1:nodes
X(i,t) = ini_q_len(t)+ K;
end
% generate values for ini_q_len using an expression (shown below is
% e.g)
ini_q_len(t+1) = ini_q_len(t)+n(t); % n if no of packets are same each slot
end
@VBBV Ok sir...I tried it like this ...could u pls check it
clc;
clear all;
close all;
T = 100; % Total slots
nodes = 8; % Total nodes
q_len = zeros(nodes,T); % Queue length
% assume 10 initially
q_len(:,1) = 10;
n = 10; % number of packets
p = 0.2; % probability of successful arrival of packets
pack_arr = binornd(n,p,[nodes,T]); % number of packets arrival in time slot 1
for t = 1:T-1 % each time slot
for i = 1:nodes
q_len(i,t+1) = q_len(i,t)+pack_arr(i,t);
%x = q_len(t)+pack_arr(i,t);
end
end
Ok, it looks fine now. So, this algorithm only determines queue length at time slot t based on the number of packets arrival at time slot t,
Yes sir....Thanks a lot for your cooperation...really learning a lot from peoples like you...
Ok, np, if you think my answer helped solved your problem, please accept it

请先登录,再进行评论。

评论:

2023-5-3

Community Treasure Hunt

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

Start Hunting!

Translated by