MATLAB Answers

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

조회 수: 3,354(최근 30일)
Ramesh Bala
Ramesh Bala 2018년 7월 3일
댓글: Walter Roberson 2021년 9월 1일
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

채택된 답변

Guillaume
Guillaume 2018년 7월 3일
편집: MathWorks Support Team 2020년 2월 27일
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
Walter Roberson
Walter Roberson 2021년 5월 5일
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().

댓글을 달려면 로그인하십시오.

추가 답변(5개)

sachin bharadwaj
sachin bharadwaj 2020년 11월 5일
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?
  댓글 수: 1
Walter Roberson
Walter Roberson 2021년 5월 5일
Whatever your PP is, it only has one column.

댓글을 달려면 로그인하십시오.


Tuyet Nhung
Tuyet Nhung 2021년 5월 20일
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?
  댓글 수: 4
Tuyet Nhung
Tuyet Nhung 2021년 5월 21일
yes! I modified and now it works fine. tks!!

댓글을 달려면 로그인하십시오.


Nurliyana Hadi
Nurliyana Hadi 2021년 6월 6일
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
Walter Roberson
Walter Roberson 2021년 6월 19일
We have no information about what a or b are or how big they are or where they came from.

댓글을 달려면 로그인하십시오.


cuong nguyen ngco
cuong nguyen ngco 2021년 6월 19일
편집: Walter Roberson 2021년 6월 19일
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
cuong nguyen ngco
cuong nguyen ngco 2021년 6월 20일
it is related to matlab version

댓글을 달려면 로그인하십시오.


Zain Achmad
Zain Achmad 2021년 8월 31일
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
Walter Roberson
Walter Roberson 2021년 9월 1일
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.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by