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

1 回表示 (過去 30 日間)
Ahmed Alshehhi
Ahmed Alshehhi 2018 年 4 月 21 日
コメント済み: Ameer Hamza 2018 年 4 月 22 日
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

回答 (1 件)

Aditya Deshpande
Aditya Deshpande 2018 年 4 月 21 日
編集済み: Aditya Deshpande 2018 年 4 月 21 日

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 件のコメント
Ahmed Alshehhi
Ahmed Alshehhi 2018 年 4 月 21 日
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 2018 年 4 月 22 日

@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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSequence and Numeric Feature Data Workflows についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by