Index in position 1 exceeds array bound. Index must not exceeds 1.?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I'm finding this error in Line 124 : dN=[dN dist(v(i,:),A(r,:))]; of my code. Can anyone tell me how to solve this problem.
clc;
clear;
close all;
tic
% Budget Constraint
% The price of each sensor is C = $3000
% The price of each monitor is C' = $122000
% The total available budget is P = $313000
CS = 3000;
CM = 122000;
P = 313000;
ln = 2; % the minimum no. of monitors
% Population Data
Pop = [44252 74524 85060 66989 94922
23631 50185 74016 80964 86887
40666 69841 65646 29660 15504
29549 21068 9487 2984 2260
4267 2293 2042 2393 1711];
add = sum(sum(Pop));
Pop=Pop/add*100; % Population Percentage
% Emission PM2.5 Data
e = [0.293849555 0.497287887 0.700726218 0.665801844 0.630877469
0.199782061 0.310923724 0.422065388 0.393195231 0.364325075
0.105714567 0.124559562 0.143404557 0.120588619 0.09777268
0.056276604 0.085150863 0.114025122 0.14243416 0.170843197
0.006838642 0.045742165 0.084645688 0.164279701 0.243913714
];
add1 = sum(sum(e));
e=e/add1*100; % Emission PM2.5 Percentage
% The empty set of Grids of point of interest (V)
v=[0 0];
A=[1 5];
s=[0 0];
B=[0 0];
H=[0 0];
t=size(Pop);
% The set of V Grids and their locations
for i=1:t(1)
for j = 1:t(2)
v(end+1,:)=[i j];
s(end+1,:)=[i j];
end
end
% This is to remove first row of v & s
v(1,:)=[];
s(1,:)=[];
B(1,:)=[];
H(1,:)=[];
% For A = empty set, d = infinite, g(d) =0
% This for loop is for averaging population and emission percentage
% for each grid
m=zeros(25,1);
for i = 1:length(v)
b=v(i,1:2);
m(i,:) = m(i,:) + (Pop(b(1),b(2))+e(b(1),b(2)))/2;
end
m;
M =[4.6314 7.8192 10.0010 8.7974 9.9390
2.8197 5.0719 7.1852 7.3060 7.3746
2.9277 4.5674 4.5058 2.4869 1.5808
1.9613 1.7624 1.4054 1.3036 1.4963
0.2728 0.4867 0.7884 1.4500 2.0590];
% For A = empty set, d = infinite, g(d) =0
% This nested for loop is to find the 1st position
% where sensor can be deployed
f1=zeros(25,1);
for j = 1:length(s)
for i = 1:length(v)
ds=dist(v(i,:),s(j,:));
dA=dist(v(i,:),A(1,:));
d=min([ds dA]);
b=v(i,1:2);
f1(j,:) = f1(j,:) + exp(-d/1)*M(b(1),b(2)) - exp(-dA/1)*M(b(1),b(2));
end
end
f1 % f(A) matrix
[fmax,grid_p] = max(f1) % find the maximum of f(A)
B = [B;2 3];
% This nested for loop is to find the 2nd position
% where sensor can be deployed
f2=zeros(25,1);
for j = 1:length(s)
for i = 1:length(v)
ds=dist(v(i,:),s(j,:));
dA1=dist(v(i,:),A(1,:));
dB1=dist(v(i,:),B(1,:));
d=min([ds dA1 dB1]);
dA=min([dA1 dB1]);
b=v(i,1:2);
f2(j,:) = f2(j,:) + exp(-d/1)*M(b(1),b(2)) - exp(-dA/1)*M(b(1),b(2));
end
end
f2 % f(A) matrix
[fmax,grid_p] = max(f2) % find the maximum of f(A)
B = [B;1 2];
% To find next sensors locations
for ii=1:23
f=zeros(25,1);
if (CS*size(A) + CM*size(B)) <= P
for j=1:length(s)
for i=1:length(v)
dN=[];
ds=dist(v(i,:),s(j,:));
for r =1:11
dN=[dN dist(v(i,:),A(r,:))];
end
dB1=dist(v(i,:),B(1,:));
dB2=dist(v(i,:),B(2,:));
d=min([ds dN dB1 dB2 dA1]);
dA=min([dN dB1 dB2 dA1]);
b=v(i,1:2);
f(j,:) = f(j,:) + exp(-d/1)*M(b(1),b(2)) - exp(-dA/1)*M(b(1),b(2));
end
end
f % f(A) matrix
[fmax,grid_p] = max(f) % find the maximum of f(A)
if mod(grid_p,5)==0
A = [A;quorem(sym(grid_p), sym(5)) 5];
else
A = [A;quorem(sym(grid_p), sym(5))+1 mod(grid_p,5)];
end
else
break
end
f =[];
end
2 Commenti
Dyuman Joshi
il 14 Ott 2023
There's another error occuring before that, see above.
As for the error, the message clearly states what's wrong.
As for the code, what are you trying to do? What is the objective?
I don't understand what you are trying to do. For e.g -
You define B and H like this
B=[0 0];
H=[0 0];
and then you delete them
B(1,:)=[];
H(1,:)=[];
What's the point of initializing then?
Risposte (1)
Pooja Kumari
il 25 Ott 2023
Hi Nishant,
It is my understanding that you are facing “Index in position 1 exceeds array bounds. Index must not exceed 1.” error in line 124 of the provided code.
As Dyuman Joshi pointed out, another error occurring before line 124 at line 84 which is “Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.” happening due to the size of ‘dA’ which is of size 1 x 2. This error can be resolved by taking one value of ‘dA’. I have replaced ‘dA’ with ‘dA(2)’ in line 84.
Updated code of line 84 is given below:
f1(j,:) = f1(j,:) + exp(-d/1)*M(b(1),b(2)) - exp(-dA/1)*M(b(1),b(2));
The error occurring in line 124 is due to value of ‘r’. “Index in position 1 exceeds array bound as the value of ‘r’ is 11 and ‘A’ is 1 dimensional row vector with 2 values. The error indicates that the index ‘r’ exceeds the size of array ‘A’.
Corrected code for error in line 124 is given below:
for r =1:2
dN=[dN dist(v(i,:),A(:,r))]; % A is a row vector with 2 column
end
A(r,:) is the error because ‘A’ is a row vector with 2 values, either value of ‘r’ can be reduced to 2 or vector ‘A’ can be redefined with 11 values.
I hope this helps!
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!