Filter löschen
Filter löschen

I find this error Index exceeds matrix dimensions can anyone help me to solve it?

1 Ansicht (letzte 30 Tage)
load project2trial.txt %data stored as column -> 7 columns with size 30x7
N = length(project2trial); % Gives number of sets of data (rows)
x=1; y=2;
P= project2trial(1:N,[x;y]);
%corresponding output data
c= project2trial(1:N,7);
w1=1.8; w2=-0.3; b=-1;a=0.5;
n=30;
for j=1:10000
R=[];
for i=1:n
s=w1*x(i)+w2*y(i)+b;
if s>=0
cc=1;
else cc=0;
end
end
end

Antworten (1)

Aditya Deshpande
Aditya Deshpande am 21 Apr. 2018
Bearbeitet: Aditya Deshpande am 21 Apr. 2018

You have the value of x and y as integer constants. If you take these as vectors, the length of these variables is 1.

Inside the for-loop of your code above, you are iterating over x and y from i = 1 to i = 30 (since, n = 30). Thus, you get the following error:

Index exceeds matrix dimensions.

I think you meant to iterate over the matrix P which you create with the data from your text file. You should do the following in this case:

load project2trial.txt
N = length(project2trial); 
x=1;
y=2;
P = project2trial(1:N, [x;y]);
w1=1.8; w2=-0.3; b=-1;a=0.5; n=30;
for j=1:10000
   R=[];
for i=1:n
    s=w1*P(i,1)+w2*P(i,2)+b;
    if s>=0 
        cc=1;
    else
        cc=0;
    end
end 
end

If not this then your requirement must be only constant values of x and y. Thus, you will have to do the following:

load project2trial.txt
N = length(project2trial); 
x=1;
y=2;
P = project2trial(1:N, [x;y]);
w1=1.8; w2=-0.3; b=-1;a=0.5; n=30;
for j=1:10000
   R=[];
for i=1:n
    s=w1*x+w2*y+b;
    if s>=0 
        cc=1;
    else
        cc=0;
    end
end 
end
  2 Kommentare
Ahmed Alshehhi
Ahmed Alshehhi am 21 Apr. 2018
first of all thank you for your help, it work when I use 1 and 2 as a value for x & y but if I change it like example using 3 & 4 it give me the same error and by the way the program should accept number from 1 to 6 as value of x & y
Ameer Hamza
Ameer Hamza am 22 Apr. 2018

@Aditya Deshpande is right. You are indexing a scalar variable x and y. Since x(2), y(2), x(3), y(3), ... does not exist, MATLAB is telling you that the "Index exceeds matrix dimensions". You will get the same error even if you use x=1; y=2 if you are using the same code you posted in the question.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Sequence and Numeric Feature Data Workflows finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by