MATLAB attempted to access x(1,3), index out of bounds because numel(x)=1.

clc
clear all
x=0;
D=zeros(154);
for i=1:248
D(x(i,1),x(i,2))=x(i,3);
end
D1=D+D';
for i=1:154
for j=1:154
if D1(i,j)==0
D1(i,j)=inf;
end
end
end
for i=1:154
for j=1:154
if i==j
D1(i,j)=0;
end
end
end
HOW IT HAPPEND= = AND I WANNA KNOW HOW TO CORRECT.DO ME A FAVOR I WILL APPRECIATE FORERVER,BECAUSE THE CODE CONNECTS WITH ONE IMPORTANT TEST.HELP!!!

7 Comments

Please format your code by highlighting it, and then clicking the '{} Code' button above. We cannot read/run your code as it is written.
x=0 creates a variable that is 1x1 (1 row, 1 column) in size. Thus, accessing x(i,3) throws an error, since x does not have 3 columns.
By the way, exactly what is your code supposed to do? By setting elements of D to x (which == 0), you are just setting D = 0. However, you already have a matrix of zeros with the D=zeros(154) line. The D1=D+D' line then gives D1 as a matrix of zeros as well.
What you you expect D1 to look like?
SORRY !!I CAN'T GET IT..I WONDER IF YOU COULD CORRECT ME= =
I've edited your code twice, and you are making it each time unreadable
so what do to do..A FRESHMAN IS A TRAGEDY

Sign in to comment.

Answers (1)

Because you set x to 0, which means its length is 1, you can not use
x(i,1)
with i>1

Asked:

on 30 Apr 2013

Community Treasure Hunt

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

Start Hunting!