# Index in position 2 exceeds array bounds (must not exceed 1) ??

1,578 views (last 30 days)
Ramesh Bala on 3 Jul 2018
Commented: Image Analyst on 16 Jan 2024
Why does the following function produce the error "Index in position 2 exceeds array bounds (must not exceed 1)"?
function test2
close all
prompt ={'Enter the x1 value: ','Enter the y1 value: ','Enter the x2 value: ','Enter the y2 value: '};
Is = inputdlg(prompt);
Is = str2double(Is);
%Eucleidan distance
D = sqrt((Is(1,2)-Is(1,1))^2 + (Is(1,4)-Is(1,3))^2);
fprintf ('the distance value obtained is %d',D)
end
##### 2 CommentsShow NoneHide None
Hedaeatul Islam Sumon on 26 Dec 2021
Can anyone help me , solve this problem?
error:
Index in position 2 exceeds array bounds. Index must not exceed 44.
Error in teamplate_matching (line 46)
x1= x1+(tem_complement(i,j)-tem_mean)*(fea_complement(a,b)-fea_mean);
Here is my code:
clc;
close all;
template = imread('AAAAA.JPG');
tem2gray = rgb2gray(template);
feature = imread('L.JPG');
fea2gray = rgb2gray(feature);
tem_thres = graythresh(tem2gray);
tem2bin = imbinarize(tem2gray,tem_thres);
fea_thresh = graythresh(fea2gray);
fea2bin = imbinarize(fea2gray,fea_thresh);
tem_complement = imcomplement(tem2bin);
fea_complement = imcomplement(fea2bin);
[height,weight] = size(tem_complement);
[height2,weight2] = size(fea_complement);
sum = 0;
for i = 1:height2
for j = 1: weight2
sum = sum + fea_complement(i,j);
end
end
fea_mean = sum/(height2*weight2);
ncc_value = zeros(1,26);
current = 1;
for x = 1:26
sum = 0;
for i = 1:height
for j =current:current+59
sum = sum+tem_complement(i,j);
end
end
tem_mean = sum/(height2*weight2);
x1 = 0;
x2 = 0;
x3 = 0;
a = 1;
for i =1:height
b= 1;
for j = current:current+59
x1= x1+(tem_complement(i,j)-tem_mean)*(fea_complement(a,b)-fea_mean);
x2 = x2 + tem_complement(i,j)^2;
x3 = x3 + fea_complement(a,b)^2;
b=b+1;
end
a= a+1;
end
c= x1/sqrt(x2*x3);
ncc_value(x) = c;
current = current+60;
end
fprintf("Value of NCC\n");
for i=1:26
fprintf('%.2f\n',ncc_value(i));
end
[value,index] = max(ncc_value);
fprintf('\n Matched With %d\n', index);
subplot(3,1,1);
imshow(tem_complement);
title('Template Image');
subplot(3,1,2);
imshow( fea_complement);
title('Cndidate Image');
subplot(3,1,3);
imshow(tem_complement);
title('Best Match shown by red rectangle box');
rectangle('Position',[(index-1)*weight2 5 weight2 height2], 'Edgecolor','r');
Walter Roberson on 8 Jan 2022
filename1 = 'AAAAA.JPG';
filename2 = 'L.JPG';
if isunix();
filename1 = 'flamingos.jpg';
filename2 = 'kobi.png';
end
template = imread(filename1);
tem2gray = rgb2gray(template);
feature = imread(filename2);
fea2gray = rgb2gray(feature);
tem_thres = graythresh(tem2gray);
tem2bin = imbinarize(tem2gray,tem_thres);
fea_thresh = graythresh(fea2gray);
fea2bin = imbinarize(fea2gray,fea_thresh);
tem_complement = imcomplement(tem2bin);
fea_complement = imcomplement(fea2bin);
[height,weight] = size(tem_complement);
[height2,weight2] = size(fea_complement);
sum = 0;
for i = 1:height2
for j = 1: weight2
sum = sum + fea_complement(i,j);
end
end
fea_mean = sum/(height2*weight2);
ncc_value = zeros(1,26);
current = 1;
for x = 1:26
sum = 0;
for i = 1:height
for j =current:current+59
if j > size(tem_complement,2)
i,j,current,whos
end
sum = sum+tem_complement(i,j);
end
end
tem_mean = sum/(height2*weight2);
x1 = 0;
x2 = 0;
x3 = 0;
a = 1;
for i =1:height
b= 1;
for j = current:current+59
if j > size(tem_complement,2) || b > size(fea_complement,2)
i, j, a, b, whos
end
x1= x1+(tem_complement(i,j)-tem_mean)*(fea_complement(a,b)-fea_mean);
x2 = x2 + tem_complement(i,j)^2;
x3 = x3 + fea_complement(a,b)^2;
b=b+1;
end
a= a+1;
end
c= x1/sqrt(x2*x3);
ncc_value(x) = c;
current = current+60;
end
i = 1
j = 1297
current = 1261
Name Size Bytes Class Attributes a 1x1 8 double b 1x1 8 double c 1x1 8 double current 1x1 8 double fea2bin 1224x1632 1997568 logical fea2gray 1224x1632 1997568 uint8 fea_complement 1224x1632 1997568 logical fea_mean 1x1 8 double fea_thresh 1x1 8 double feature 1224x1632x3 5992704 uint8 filename1 1x13 26 char filename2 1x8 16 char height 1x1 8 double height2 1x1 8 double i 1x1 8 double j 1x1 8 double ncc_value 1x26 208 double sum 1x1 8 double tem2bin 972x1296 1259712 logical tem2gray 972x1296 1259712 uint8 tem_complement 972x1296 1259712 logical tem_mean 1x1 8 double tem_thres 1x1 8 double template 972x1296x3 3779136 uint8 weight 1x1 8 double weight2 1x1 8 double x 1x1 8 double x1 1x1 8 double x2 1x1 8 double x3 1x1 8 double
Index in position 2 exceeds array bounds. Index must not exceed 1296.
So you start current at 1 in the outer loop, and you add 60 to it each iteration. You have 29 iterations so you are assuming that your data can be indexed to at least 1+60*28 = 1681 based upon current.
Inside your x loop you loop j =current:current+59 so you assume that you can index up to 1+60*28+59 = 60*29 = 1740 .
But suppose that is not the case: suppose the image you read in is smaller than that. Why are you hard-coding 29 loops of x? Why are you not determining the upper limit according to the size of the image?
Remember, by the way, to take into account that your image might not be an exact multiple of 60 wide.
I distinctly remember posting some other hints for you about how to calculate your values more efficiently, but I cannot find those at the moment.

Sign in to comment.

### Accepted Answer

Guillaume on 3 Jul 2018
Edited: MathWorks Support Team on 27 Feb 2020
The error occurs because the vector “Is” is a 4-by-1 column vector (not a 1-by-4 row vector) so that the valid element positions are (1,1), (2,1), (3,1), and (4,1). If you try to access any index value beyond 1 in the 2nd position, you will encounter the error.
##### 3 CommentsShow 1 older commentHide 1 older comment
Frisda Sianipar on 28 Apr 2021
Index in position 2 exceeds array bounds (must not exceed 1).
Error in KNN (line 3)
group=latih(:,3);
I got the error
this is the code:
x=xlsread("datatraining.xlsx");
latih=x;
group=latih(:,3);
latih = [latih(:,1) latih(:,2)];
for i = 1 : 80
y=xlsread("datatesting.xlsx");
sampel = y;
test = [sampel(:,1) sampel(:,2)];
%sampel = [2.6136 0.1284 1.3017 -0.8089 0.0441 -0,2084];
hasil=knnclassify(test,latih,group);
end
nama = "hasil KNN.xlsx";
hasil = [sampel(:,1) sampel(:,2) sampel(:,3) hasil];
xlswrite(nama,hasil);
can you help me to solve the problem?
Walter Roberson on 5 May 2021
When you use xlsread(), the first output is trimmed down to remove all leading and trailing rows and columns that are all NaN after conversion to numbers. Typically that happens for header rows and for leading or trailing text columns. You first column probably has text in it, so it became all NaN when converted to numbers, and then got discarded. That would have thrown off your indexing.
We recommend that you switch to readtable().

Sign in to comment.

### More Answers (8)

sachin bharadwaj on 5 Nov 2020
Index in position 2 exceeds array bounds (must not exceed 1).
Error in fatlabexam (line 17)
g=PP(:,1)*h(1)+PP(:,2)*h(2);
why is it showing error?
##### 3 CommentsShow 1 older commentHide 1 older comment
Hedaeatul Islam Sumon on 26 Dec 2021
Can anyone help me , solve this problem?
error:
Index in position 2 exceeds array bounds. Index must not exceed 44.
Error in teamplate_matching (line 46)
x1= x1+(tem_complement(i,j)-tem_mean)*(fea_complement(a,b)-fea_mean);
Here is my code:
ThemeCopy
clc;
close all;
template = imread('AAAAA.JPG');
tem2gray = rgb2gray(template);
feature = imread('L.JPG');
fea2gray = rgb2gray(feature);
tem_thres = graythresh(tem2gray);
tem2bin = imbinarize(tem2gray,tem_thres);
fea_thresh = graythresh(fea2gray);
fea2bin = imbinarize(fea2gray,fea_thresh);
tem_complement = imcomplement(tem2bin);
fea_complement = imcomplement(fea2bin);
[height,weight] = size(tem_complement);
[height2,weight2] = size(fea_complement);
sum = 0;
for i = 1:height2
for j = 1: weight2
sum = sum + fea_complement(i,j);
end
end
fea_mean = sum/(height2*weight2);
ncc_value = zeros(1,26);
current = 1;
for x = 1:26
sum = 0;
for i = 1:height
for j =current:current+59
sum = sum+tem_complement(i,j);
end
end
tem_mean = sum/(height2*weight2);
x1 = 0;
x2 = 0;
x3 = 0;
a = 1;
for i =1:height
b= 1;
for j = current:current+59
x1= x1+(tem_complement(i,j)-tem_mean)*(fea_complement(a,b)-fea_mean);
x2 = x2 + tem_complement(i,j)^2;
x3 = x3 + fea_complement(a,b)^2;
b=b+1;
end
a= a+1;
end
c= x1/sqrt(x2*x3);
ncc_value(x) = c;
current = current+60;
end
fprintf("Value of NCC\n");
for i=1:26
fprintf('%.2f\n',ncc_value(i));
end
[value,index] = max(ncc_value);
fprintf('\n Matched With %d\n', index);
subplot(3,1,1);
imshow(tem_complement);
title('Template Image');
subplot(3,1,2);
imshow( fea_complement);
title('Cndidate Image');
subplot(3,1,3);
imshow(tem_complement);
title('Best Match shown by red rectangle box');
rectangle('Position',[(index-1)*weight2 5 weight2 height2], 'Edgecolor','r');
Walter Roberson on 8 Jan 2022

Sign in to comment.

Tuyet Nhung on 20 May 2021
Index in position 2 exceeds array bounds.
Error in m20 (line 7)
t = ndata(:,1); Vab = ndata(:,2); VR = ndata(:,3); Ir = VR/45;
Here is the code:
function m20
clc
h = figure;
set(h,'position',[10,10,600,680],'numbertitle','off','color', 'w')
ndata = xlsread('..\PD\Vab_Vr\20.csv');
R = 45;
t = ndata(:,1); Vab = ndata(:,2); VR = ndata(:,3); Ir = VR/45;
%Ve Vcm
subplot( 2,1,1), plot(t,Vab ,'color',[1,0.5,0]); grid,
ylabel('Mag (V)','fontname','vni-times','fontsize',15),
title('a) V_a_b','fontname','vni-times','fontsize',20),
xlabel('Time (seconds)','fontname','vni-times','fontsize',15)
%Ve Van
subplot( 2,1,2), plot(t,Ir ,'color',[1,0,0.8]); grid, axis([-0.03,0.03,-2,2])
ylabel('Mag (V)','fontname','vni-times','fontsize',15),
title('b) I_R','fontname','vni-times','fontsize',20),
xlabel('Time (seconds)','fontname','vni-times','fontsize',15)
Can you help me to solve the problem?
##### 19 CommentsShow 17 older commentsHide 17 older comments
Preeti Panchta on 22 Sep 2022
if im applying this then it will give only 2d pareto front not 3d pareto front
Preeti Panchta on 22 Sep 2022
my main question is how to plot these objectives in 3d pareto front

Sign in to comment.

Nurliyana Hadi on 6 Jun 2021
can anyone help me to solve this??
Index in position 2 exceeds array bounds.
Error in Example3_cmp (line 15)
plot(a(:,1)-1,a(:,2));
--------------------------------------------------------------
param =34473.8;
plot(a(:,1)-1,a(:,2))
hold on
plot(node(:,1)-1,(nodefc1(:,2)-node(:,2))/(0.01*param),'b--','linewidth',2)
plot(node(:,1)-1,(nodefc2(:,2)-node(:,2))/(0.001*param),'g','linewidth',2)
plot(node(:,1)-1,(nodefc3(:,2)-node(:,2))/(0.0001*param),'k','linewidth',2)
plot(node(:,1)-1,(nodefc4(:,2)-node(:,2))/(0.00001*param),'r:','linewidth',2)
legend('DDM','FFD 0.01','FFD 0.001','FFD 0.0001','FFD 0.00001')
xlabel('Time [sec]')
ylabel('\partialu_6/\partialfc')
set(get(gca,'ylabel'),'Fontsize',14)
set(get(gca,'xlabel'),'Fontsize',14)
set(gca,'Fontsize',14)
figure (2)
load node.out
b = load('node_sens6.out');
load nodeE1.out
load nodeE2.out
load nodeE3.out
%load nodeE4.out
param =2.1e8;
plot(b(:,1)-1,b(:,2))
hold on
plot(node(:,1)-1,(nodeE1(:,2)-node(:,2))/(0.0005*param),'b--','linewidth',2)
plot(node(:,1)-1,(nodeE2(:,2)-node(:,2))/(0.0003*param),'g','linewidth',2)
plot(node(:,1)-1,(nodeE3(:,2)-node(:,2))/(0.0001*param),'k','linewidth',2)
%plot(node(:,1)-1,(nodeE4(:,2)-node(:,2))/(0.00001*param),'r:')
##### 1 CommentShow -1 older commentsHide -1 older comments
Walter Roberson on 19 Jun 2021
We have no information about what a or b are or how big they are or where they came from.

Sign in to comment.

cuong nguyen ngco on 19 Jun 2021
Edited: cuong nguyen ngco on 19 Jun 2021
can you help me for error Index in position 2 exceeds array bounds (must not
exceed 1) with trains = data(:,Tr_ind);
%main function main
load('ORL_32x32.mat')%importing face data
%%split datta
data=load('ORL_32x32.mat');
label=unique(gnd);
TrainNum=5;
Tr_ind=[];
Te_ind=[];
for i=1:length(label)
tempind=find(gnd==label(i));
Tr_ind=[Tr_ind,tempind(1:TrainNum)];
Te_ind=[Te_ind,tempind(TrainNum+1:end)];
end
%%label and data
Train_label=gnd(Tr_ind);
Test_label=gnd(Te_ind);
Trains=data(:,Tr_ind);
##### 6 CommentsShow 4 older commentsHide 4 older comments
Walter Roberson on 19 Jun 2021
%main function main
ORL_struct = load('ORL_32x32.mat'); %importing face data
gnd = ORL_struct.gnd;
data = ORL_struct.data;
%%split datta
label=unique(gnd);
TrainNum=5;
Tr_ind=[];
Te_ind=[];
for i=1:length(label)
tempind=find(gnd==label(i));
Tr_ind=[Tr_ind,tempind(1:TrainNum)];
Te_ind=[Te_ind,tempind(TrainNum+1:end)];
end
%%label and data
Train_label=gnd(Tr_ind);
Test_label=gnd(Te_ind);
Trains=data(:,Tr_ind);
cuong nguyen ngco on 20 Jun 2021
it is related to matlab version

Sign in to comment.

Zain Achmad on 31 Aug 2021
can anyone help me to solve this?
Index in position 2 exceeds array bounds.
Error in klasifikasi_svm (line 82)
PC1 = score_latih(:,1);
-------------------------------------------------------------------------------
nama_folder = 'data latih';
nama_file = dir(fullfile(nama_folder,'*.jpg'));
jumlah_file = numel(nama_file);
% inisialisasi variabel ciri_latih
ciri_latih = zeros(jumlah_file,4);
for n = 1:jumlah_file
% membaca citra RGB
Img = imread(fullfile(nama_folder,nama_file(n).name));
% konversi citra RGB menjadi grayscale
Img_gray = rgb2gray(Img);
% konversi citra grayscale menjadi biner
bw = im2bw(Img_gray,graythresh(Img_gray));
% operasi morfologi
bw = imcomplement(bw);
bw = imfill(bw,'holes');
bw = bwareaopen(bw,100);
% ekstraksi ciri warna HSV
HSV = rgb2hsv(Img);
H = HSV(:,:,1);
S = HSV(:,:,2);
V = HSV(:,:,3);
H(~bw) = 0;
S(~bw) = 0;
V(~bw) = 0;
Hue = sum(sum(H))/sum(sum(bw));
Saturation = sum(sum(S))/sum(sum(bw));
Value = sum(sum(V))/sum(sum(bw));
% ekstraksi ciri ukuran
Area = sum(sum(bw));
% mengisi hasil ekstraksi ciri pada variabel ciri_latih
ciri_latih(n,1) = Hue;
ciri_latih(n,2) = Saturation;
ciri_latih(n,3) = Value;
ciri_latih(n,4) = Area;
end
% standarisasi data
[ciri_latihZ,muZ,sigmaZ] = zscore(ciri_latih);
% pca
[coeff,score_latih,latent,tsquared,explained] = pca(ciri_latihZ);
% inisialisasi variabel kelas_latih
kelas_latih = cell(jumlah_file,1);
% mengisi nama2 sayur pada variabel kelas_latih
for k=1:300
kelas_latih{k} = 'Angry';
end
for k=301:600
kelas_latih{k} = 'Disgust';
end
for k=601:900
kelas_latih{k} = 'Fear';
end
for k=901:1200
kelas_latih{k} = 'Happy';
end
for k=1201:1500
kelas_latih{k} = 'Neutral';
end
for k=1501:1800
kelas_latih{k} = 'Sad';
end
for k=1801:2100
kelas_latih{k} = 'Surprise';
end
% ekstrak PC1 & PC2
PC1 = score_latih(:,1);
PC2 = score_latih(:,2);
PC3 = score_latih(:,3);
PC4 = score_latih(:,4);
PC5 = score_latih(:,5);
% klasifikasi menggunakan
Mdl = fitcecoc([PC1,PC2,PC3,PC4,PC5],kelas_latih);
% menyimpan variabel-variabel hasil pelatihan
save hasil_pelatihan Mdl muZ coeff sigmaZ
-----------------------------------
##### 3 CommentsShow 1 older commentHide 1 older comment
Zain Achmad on 1 Sep 2021
Edited: Zain Achmad on 1 Sep 2021
local folder is contains 2100 images and its named with 7 expression each 300 images name, is that what makes the error ? before used 2100 i use 70 images for experiment and the program is works fine but when I added more images and the error appeared
Walter Roberson on 1 Sep 2021
What is the value of
jumlah_file
size(ciri_latih)
size(score_latih)
I suspect that your current directory is not the directory that the folder 'data latih' is inside. I suspect that your current directory is 'data latih' itself rather than the directory that contains 'data latih' as your code requires.

Sign in to comment.

Feyza Zehra Asik on 28 Dec 2021
Can you help with same error Index in position 2 exceeds array bounds (must not exceed 1). in line 39
clc
close all
clear all
img=imread('resim1.jpg');
img=imcrop(img,[5 2 725 584]);
imtool('resim1.jpg')
figure(1), imshow(img);
title('savunma oyuncusu');
img_h=rgb2hsv(img);
h=img_h(:,:,1);
s=img_h(:,:,2);
v=img_h(:,:,3);
level=graythresh(img);
[r c]=size(h);
for i=1:r
for j=1:c
if (h(i,j)>=0.01 && h(i,j)<=0.99)
out(i,j)=255;
else
out(i,j)=0;
end
end
end
t=bwareaopen(out,300);
se=strel('disk',0);
bw=imclose(t,se);
[B,L]= bwboundaries(bw,'noholes'),disp(B);
hold on
for k = 1:length(B)
boundary=B(k);
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth',2)
end
fprintf('Nesneler isaretlenmistir. Toplam nesne sayisi=%d\n', length(B))
stats=regionprops(L,'Area','Centroid');
figure(2), imshow(img);
[x,y]=find(imcomplement(bw)==0);
img1=imread('resim1.jpg');
img1=imcrop(img1,[5 2 745 594]);
figure(3), imshow(img1);title('hucum oyunculari');
img1_h1=rgb2hsv(img1);
h1=img1_h1(:,:,1);
s1=img1_h1(:,:,2);
v1=img1_h1(:,:,3);
level1=graythresh(img1);
[r c]=size(h1);
for i=1:r
for j=1:c
if s1(i,j)>=0.17 && s1(i,j)<=0.23
out1(i,j)=255;
else
out1(i,j)=0;
end
end
end
%figure, imshow(out)
t1=bwareaopen(out1,200)
%figure, imshow(t)
se=strel('disk',0);
bw1=imclose(t1,se);
[B,L]=bwboundaries(bw1,'noholes'),disp(B);
hold on
for k=1:length(B)
boundary1=B(k);
plot(boundary1(:,2), boundary1(:,1), 'w', 'LineWidtyh',2)
end
fprintf('nesneler isaretlenmistir. Toplam nesne sayisi=%d\n',length(B));
stats=regionprops(L,'Area','Centroid');
%for k=1:length(B) % nesneleri sayan loopun baslangici
[x1,y1]=find(imcomplement(bw1)==0)
d=min(x)+357;
d1=min(x1)+9;
a=(d-d1);
if(a>0)
for j=d
for i=105:405
img(i,j)=255;
end
end
figure,imshow(img);title('ofsayt');
else
for j=d1
for i=1:20
img(i,j)=255;
end
end
figure,imshow(img);title('ofsayt yok');
end
##### 1 CommentShow -1 older commentsHide -1 older comments
Walter Roberson on 28 Dec 2021
boundary1=B(k);
should be
boundary1 = B{k};

Sign in to comment.

Ahmad Aakash on 8 Jan 2022
Edited: Walter Roberson on 8 Jan 2022
can any one help me with this error. thanks in advance
Index in position 2 exceeds array bounds. Index must not exceed 26.
Error in fit_eq_5_coeffs (line 97)
p_exp_ind = p_exp.data(:,exp_ind);
the code is following
function [c, beta_SO] = fit_eq_5_coeffs(groupflag,plotflag,output_path,prompt_output)
% order of parameters: A B S E V L const X
Y = load('../model_parameters/Abraham_parms_training_set.dat');
group = Y(:,7);
% In case of groupflag set to zero, select all data for training.
% Otherwise use only the subset that matches the groupflag value.
if groupflag == 0
group = group*0;
end
Y = Y(group==groupflag,:);
p_exp_all = importdata('../model_parameters/expt_properties_training_set.dat','\t');
p_exp.data = p_exp_all.data(group==groupflag,:);
X = Y(:,1).*Y(:,2); % "interaction" term for pure liquid properties pl and sw
n = length(X);
Y = [Y(:,1:6) ones(n,1) X];
y = load('../model_parameters/Abraham_coeffs.dat');
% indices:
% 1 SE-30
% 2 OV-17
% 3 pL
% 4 delta Hvap
% 5 Kha
% 6 Koa
% 7 Koc-a
% 8 Kaw
% 9 SwL
% 10 Kow
% 11 Koc-w
% 12 Kdoc-w
% 13 BCF
% 14 Khw
% 15 Knw
% 16 Ktw
% 17 KPDMS-w
% 18 KPDMS-a
% 19 KPOM-w
% 20 KPA-w
% 21 KPU–a
% 22 KSPMD-w
% 23 KS
% 24 Kph-w
% 25 Ks.lip-w
% 26 Ks.lip-a
% 27 Kalbumin-w
% 28 Kmuscle.protein-w
% 29 -logLC50
% 30 Dw
% 31 D_ethanol
% 32 logKp
% 33 log(1/EIT)
% 34 log(P0/D)
% 35 log(1/NPT)
% 36 log(1/ODT)
% 37 log(1/MAC)
% 38 log(1/RD50)
% 39 log(1/VOL)
% 40 log(1/C)
% 41 log(1/CON)
% computation of hypothetical partition constant values
p_1 = sum(Y.*(ones(n,1)*y(1,:)),2);
p_2 = sum(Y.*(ones(n,1)*y(2,:)),2);
% save logL1 and logL2 values of the training set
logL12_training_set = [p_1 p_2];
savefilename = strcat('../',output_path,'logL12_training_set.dat');
save(savefilename,'logL12_training_set','-ascii','-tabs');
% Define u_1 and u_2 as two orthogonal bases of p_1 and p_2. First set:
u_1 = p_1;
% then determine a constant beta_SO relating p_1 and p_2 via Schmidt
% Orthogonalization of the system <u_1*(u_1-beta_SO*p_2)> = 0, which is:
beta_SO = sum(p_1.*p_2)/sum(p_1.^2);
% where for the orthogonal vectors <u_1*u_2> = 0
u_2 = p_2-beta_SO*p_1;
% fit eq 5 coeffs for properties for which experimental data is available
y_ind = [3 4 5 6 7 8 9 10 11 12 13 17:41];%###########
Nboot = 1000;
for exp_ind = 1:length(y_ind)
p(:,exp_ind) = sum(Y.*(ones(n,1)*y(y_ind(exp_ind),:)),2);
% substitute experimental data, where available
p_exp_ind = p_exp.data(:,exp_ind);
p(p_exp_ind==p_exp_ind,exp_ind)=p_exp_ind(p_exp_ind==p_exp_ind);
[c(exp_ind,:), c_CI, p_pred(:,exp_ind), r2(exp_ind), r2_unc] = svd_regress_boot([u_1 u_2 ones(n,1)],p(:,exp_ind),Nboot);
c_unc(exp_ind,:) = mean(c_CI);
if c_unc(exp_ind,2) > abs(c(exp_ind,2))
[c_tmp, c_CI_tmp, p_pred(:,exp_ind), r2(exp_ind), r2_unc] = svd_regress_boot([u_1 ones(n,1)],p(:,exp_ind),Nboot);
c(exp_ind,:) = [c_tmp(1) 0 c_tmp(2)];
c_CI = [c_CI_tmp(:,1) zeros(2,1) c_CI_tmp(:,2)];
c_unc(exp_ind,:) = mean(c_CI);
end
% obtain rmse values for the eq 5 fit
rmse(exp_ind) = sqrt(sum((p_pred(:,exp_ind)-p(:,exp_ind)).^2)./length(p(:,exp_ind)));
if plotflag == 2
scrsz = get(0,'ScreenSize');
figure('Position',[1 1 scrsz(3)/2.5 scrsz(4)/2]);
plot(p(:,exp_ind),p_pred(:,exp_ind),'g^');
hold on; plot(min(p(:,exp_ind)):0.1:max(p(:,exp_ind)),min(p(:,exp_ind)):0.1:max(p(:,exp_ind)),'k--');
hold off;
xlabel('Experimental or ASM-predicted Value');
ylabel('Fitted Value');
switch exp_ind
case 1
title('Fitted log p_L Values (Pa) of the Training Set Using Eq 5');
case 2
title('Fitted Enthalpy of Vaporization Values (kJ/mol) of the Training Set Using Eq 5');
case 3
title('Fitted log K hexadecane-air Values of the Training Set Using Eq 5');
case 4
title('Fitted log K octanol-air Values of the Training Set Using Eq 5');
case 5
title('Fitted log K organic carbon-air Values of the Training Set Using Eq 5');
case 6
title('Fitted log K air-water Values of the Training Set Using Eq 5');
case 7
title('Fitted log S_w_L Values (mol/m3) of the Training Set Using Eq 5');
case 8
title('Fitted log K octanol-water Values of the Training Set Using Eq 5');
case 9
title('Fitted log K amorphous organic carbon-water Values of the Training Set Using Eq 5');
case 10
title('Fitted log K dissolved organic carbon-water Values of the Training Set Using Eq 5');
case 11
title('Fitted Bioconcentration Factor Values of the Training Set Using Eq 5');
case 12
title('Fitted KPDMS-w Values of the Training Set Using Eq 5');
case 13
title('Fitted KPDMS-a Values of the Training Set Using Eq 5');
case 14
title('Fitted KPOM-w Values of the Training Set Using Eq 5');
case 15
title('Fitted KPA-w Values of the Training Set Using Eq 5');
case 16
title('Fitted KPU–a Values of the Training Set Using Eq 5');
case 17
title('Fitted KSPMD-w Values of the Training Set Using Eq 5');
case 18
title('Fitted KS Values of the Training Set Using Eq 5');
case 19
title('Fitted Kph-w Values of the Training Set Using Eq 5');
case 20
title('Fitted Ks.lip-w Values of the Training Set Using Eq 5');
case 21
title('Fitted Ks.lip-a Values of the Training Set Using Eq 5');
case 22
title('Fitted Kalbumin-w Values of the Training Set Using Eq 5');
case 23
title('Fitted Kmuscle.protein-w Values of the Training Set Using Eq 5');
case 24
title('Fitted -logLC50 Values of the Training Set Using Eq 5');
case 25
title('Fitted Dw Values of the Training Set Using Eq 5');
case 26
title('Fitted D_ethanol Values of the Training Set Using Eq 5');
case 27
title('Fitted logKp Values of the Training Set Using Eq 5');
case 28
title('Fitted log(1/EIT) Values of the Training Set Using Eq 5');
case 29
title('Fitted log(D/P0) Values of the Training Set Using Eq 5');
case 30
title('Fitted log(1/NPT) Values of the Training Set Using Eq 5');
case 31
title('Fitted log(1/ODT) Values of the Training Set Using Eq 5');
case 32
title('Fitted log(1/MAC) Values of the Training Set Using Eq 5');
case 33
title('Fitted log(1/RD50) Values of the Training Set Using Eq 5');
case 34
title('Fitted log(1/VOL) Values of the Training Set Using Eq 5');
case 35
title('Fitted log(1/C) Values of the Training Set Using Eq 5');
case 36
title('Fitted log(1/CON) Values of the Training Set Using Eq 5');
end
end
end
c_unc(c==0) = 0;
all_eq5_statistics = [c c_unc rmse' r2'];
%disp('Eq 5 Training Set Fit Statistics are as Follows:');
%disp(' lam1 lam2 lam3 pm_lam1 pm_lam2 pm_lam3 RMSE r^2');
%disp(all_eq5_statistics);
if strcmp(prompt_output,'verbose')
property_names = importdata('../model_parameters/properties_list.txt');
disp('Eq 5 Training Set Fit Statistics are as Follows:');
disp('r^2 RMSE property');
for ind_prop = 1:length(rmse)
str1 = sprintf('%1.2f ',r2(ind_prop));
str2 = sprintf('%2.2f ',rmse(ind_prop));
disp([str1,str2,property_names{ind_prop}]);
end
disp(' ');
end
savefilename = strcat('../',output_path,'eq5_training_set_fit_statistics.dat');
save(savefilename,'all_eq5_statistics','-ascii');
% correlation of u_1 with u_2
r2_u1_u2_training_set = corrcoef(u_1-mean(u_1),u_2-mean(u_2)).^2;
if strcmp(prompt_output,'verbose')
disp('The correlation of u_1 and u_2 of the training set is described by r^2 of:');
disp(r2_u1_u2_training_set(1,2))
end
u12 = [u_1 u_2];
savefilename = strcat('../',output_path,'u1_u2_values_training_set.dat');
save(savefilename,'u12','-ascii');
##### 2 CommentsShow NoneHide None
Mohamed Abdul-Al on 14 Aug 2022
Edited: Walter Roberson on 20 Sep 2022
Hi, I have this error and I couldn't solve it. Any help, please!
Error is:
In Train_QDC_Classifier (line 7)
Index in position 2 exceeds array bounds.
Here is the code:
clc
clear
addpath prtools\
load Curvelet_Fractal_Features
load Labels
[Data] = (Curvelet_Fractal_Features);
[TrainingSet,~,TrainingLabels,~]=DataSplite(Data,Labels,0.1,3);
TrainSet=prdataset(TrainingSet,TrainingLabels);
R=1; % between 0 to 1
S=1; % between 0 to 1
Max_Accurcy=0;
for R_now=0:0.1:1
for S_now=0:0.1:1
QDC_Classifier=qdc([],R_now,S_now);
% Apply 10-folds cross-validation evaluation procedure
ERR = prcrossval(TrainSet,QDC_Classifier,10,1);
Walter Roberson on 14 Aug 2022
if line 7 is
[TrainingSet,~,TrainingLabels,~]=DataSplite(Data,Labels,0.1,3);
then DataSplite is a variable instead of a function and Labels contains values larger than the maximum second dimension of the variable.

Sign in to comment.

Taha on 16 Jan 2024
imageFile=dir('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training*.png');
for i=1:length(imageFile)
filename=strcat('CC:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\',imageFile(i).name);
I=imread(filename);
gray=rgb2gray(I);
%figure,imshow(I);
path=strcat('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\bin\',imageFile(i).name);
imwrite(gray,path)
level = graythresh(gray);
BW = imbinarize(gray,level);
convert='.bmp';
path2=strcat('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\bin\',imageFile(i).name,convert);
imwrite(BW,path2)
imageFile2=dir('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\bin\*.bmp');
filename2=strcat('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\bin\',imageFile2(i).name);
II=imread(filename2);
SE = strel("disk",7);
erosion=imerode(II,SE);
dilation= imdilate(erosion,SE);
ED= 'EroDil';
path3=strcat('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\bin\',ED ,imageFile2(i).name);
imwrite(dilation,path3)
filename3=strcat('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\bin\',ED ,imageFile2(i).name);
III=imread(filename3);
GLCM = graycomatrix(III);
stats = graycoprops(GLCM,'contrast Correlation Energy Homogeneity');
Contrast = stats.Contrast;
Correlation = stats.Correlation;
Energy = stats.Energy;
Homogeneity = stats.Homogeneity;
features = [Contrast, Correlation, Energy, Homogeneity]
end
datasetsudu=xlsread('Part1.xlsx','sheet1','A29:B1');
xs=datasetsudu(:,1);
ys=datasetsudu(:,2);
datasetpisau=xlsread('Part1.xlsx','Sheet2','A31:B1');
xp=datasetpisau(:,1);
yp=datasetpisau(:,2);
scatter(xs,ys,'MarkerFaceColor',[0 0 1]);
hold all;
scatter(xp,yp,'MarkerFaceColor',[1 0 0]);
grid on;
xlabel('Centroid');
ylabel('Perimeter');
title('Centroid vs Perimeter');
function datasetsudu = Part3(i)
T = readtable('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\feature_data.csv');
X = table2array(T(:,2:4));
figure;
scatter(X(1:29,1),X(1:29,2),12,'r','filled'); hold on
scatter(X(30:60,1),X(30:60,2),12,'b','filled'); title('Area Against Perimeter'); xlabel('Area');
ylabel('Perimeter');
hold off
for i = 1:29
A1{i} = X(i,1); P1{i} = X(i,2);
end
Array_A1 = cell2mat(A1); Array_P1 = cell2mat(P1); Centroid_A1 = mean(Array_A1); Centroid_P1 =mean(Array_P1);
for i = 1:31
A2{i} = X(i+29,1); P2{i} = X(i+29,2);
end
Array_A2 = cell2mat(A2); Array_P2 = cell2mat(P2);
Centroid_A2 = mean(Array_A2); Centroid_P2 = mean(Array_P2);
C = [Centroid_A1 Centroid_P1;Centroid_A2 Centroid_P2]
T2 = readtable('C:\Users\HP\Downloads\machin vasion\Machine Vision-20231115T135345Z-001\Machine Vision\Lemon\Training\feature_data.csv');
Xtest = table2array(T2(:,2:3));
[~,idx_test] = pdist2(C,Xtest,'euclidean','Smallest',1);
end
this also show with me same problem can anyone help me to solve it.
the error show:
Index in position 2 exceeds array bounds.
Error in Lab3_1 (line 34)
xs=datasetsudu(:,1);
##### 1 CommentShow -1 older commentsHide -1 older comments
Image Analyst on 16 Jan 2024
Please start your own question, in a new and different thread, after you look at this:
Evidently datasetsudu is null. Do this:
>> whos datasetsudo

Sign in to comment.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2018a

### Community Treasure Hunt

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

Start Hunting!