Index in position 1 exceeds array bounds. Error in dt (line 41) del(i,N) = del(i,N-i);

1 visualizzazione (ultimi 30 giorni)
clc;
clear all;
close all;
n=100;
r=10;
a=1;
b=3;
p=[];
q=[];
del=[];
d_t=[];
for i = 1:n
%p(i) = i
%q(i) = n - p(i)
for N = 1:r
p(i,N) = i;
q(i,N) = n - p(i,N);
%disp(p(i,N))
%disp(q(i,N))
%disp("The forwarding ratio is, fr = ");
fr(i,N) = p(i,N)/(p(i,N)+q(i,N));
%disp(p(i,N))
%disp(q(i,N))
disp("The number of packets transmitted are, p = ");
disp(p(i,N))
disp("The number of packets dropped are, q = ");
disp(q(i,N))
disp("The forwarding ratio is, fr = ");
disp(fr(i,N))
while N > 1
if fr(i,N-i) > fr(i,N)
del(i,N) = del(i,N-i) + a*(fr(i,N-i) - fr(i,N));
disp("The values of the 'del' parameter are, deln = ")
disp(del(i,N))
end
if fr(i,N-i) < fr(i,N)
del(i,N) = del(i,N-i) + b*(fr(i,N-i) - fr(i,N));
disp("The values of the 'del' parameter are, deln = ")
disp(del(i,N))
else
del(i,N) = del(i,N-i);
disp("The values of the 'del' parameter are, deln = ")
disp(del(i,N))
end
d_t = fr(i,N) * cos((22/14)*del(i,N));
%disp("The direct trust values are ,d_t = ")
%disp(d_t)
end
%d_t = fr(i,N) * cos((22/14)*del(i,N));
disp("The direct trust values are ,d_t = ")
disp(d_t)
end
end

Risposte (1)

KSSV
KSSV il 9 Set 2022
This error occurs, when you try to extxract more number of elements than present in the array.
% Example
A = [1 2] ;
A(1) % no error
ans = 1
A(2) % no error
ans = 2
A(3) % error, becuase there are only two elements.
Index exceeds the number of array elements. Index must not exceed 2.
Similarly, in your case the variable del is of size 1x1 and you are extracting del(1,N) with N = 2. So error pops.
Think your problem and code it properly. You should initialize the arrays inside the loop well before the loop.

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Tag

Prodotti


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by