??? Index exceeds matrix dimensions.

2 views (last 30 days)
Yaojiayin
Yaojiayin on 15 Sep 2014
Commented: Yaojiayin on 17 Sep 2014
I have a matlab file (m-file) with the operation for spatial regression from my teacher (I also have a .txt file data333 for all data of sample points), it looks like this:
%*********Spatiell regression******** %***This program calculates a new value (z*) for each value (zi)in a table and %***account for all other values (zj). These other values (zj)are weighted with the %****squared distance (1/d^2). %***In this version, we only use values that are within a specific radius %***chosen by the user. e.g "d<30" %***The program calculates new z for the same number of columns that the input file has got. %*** % % % % % clear format long % load C:\tempdata\data333.txt col=0; raa=0.05; d=0; c=0; n=0; w=0; b=0; data4=zeros(220,10);
for col=3:12
for i=1:220
b=0;
c=0;
for j=1:220
if i~=j
d=sqrt((data333(i,1)-data333(j,1))^2+(data333(i,2)-data333(j,2))^2);
if d<30
c=(1/(d^2))*data333(j,col);
b=b+c;
else
end
end
end
x=data333(i,col) - (raa * b);
n=i;
data4(i,col)= x;
data4(i,1)=n;
end
% dlmwrite('trash.txt',data4,'\t') dlmwrite('data333-100spreg2-raa05.txt',data4,'\t') end
I thus just copied those steps except I wrote data333=importdata ('C:\tempdata\data333.txt') instead of use Load. This is because everytime I use Load, I got error messages :"Undefined function or variable xxx" or "Undefined function or method 'data333' for input arguments of types 'double' ” etc.
After I wrote data333=importdata ('C:\tempdata\data333.txt')I got: data333 =
data: [220x12 double]
textdata: {'x' 'y' 'DEM' 'asp' 'grad' 'DAI2' 'HI' 'VI' 'SLI2' 'Yield' 'DAI1' 'SLI1'}
colheaders: {'x' 'y' 'DEM' 'asp' 'grad' 'DAI2' 'HI' 'VI' 'SLI2' 'Yield' 'DAI1' 'SLI1'}
Then I copied the rest and ran MATLAB, an error message appeared: Index exceeds matrix dimensions.
I never used MATLAB before and suddently I have to hand in this assignment within 3 days! Could somebody help me to figure it out? Thanks!!!
Yaoyao

Answers (1)

Roger Stafford
Roger Stafford on 16 Sep 2014
I suspect the trouble lies with 'data333'. It may not have as many as 12 columns. If not, the lines "c=(1/(d^2))*data333(j,col);" and "x=data333(i,col) - (raa * b);" would be accessing non-existent data in 'data333'. To check this ask for size(data333) after it is loaded.
Also the line "data4(i,col)= x;" indicates an unplanned expansion of 'data4' which was originally only provided with 10 columns. There's something fishy there.
  9 Comments
Hikaru
Hikaru on 17 Sep 2014
Make sure you input the correct filename when using readtable, I used exercise-data333.txt because it was provided in your zip file, but I don't really know which files you were supposed to use. I also used T as a random variable name, it looks like in your program, it should be named as data333 ?
And like Star mentioned above, you will have to convert the table into a matrix by using the table2array function.
I ran it on my machine and it didn't produce any error.
Yaojiayin
Yaojiayin on 17 Sep 2014
1. My readtable code is: T = readtable('exercise-data333.txt','Delimiter','\t') Then I created a table T for exercise-data333.txt. But if write size T, returns ans=1 1. If do not use T but data333 as the tablename, size data333 returns ans=1 7. Last night I indeed got ans=219 12(because of 'HeaderLines',1 ) after created table. Where did I do wrong today? I'm sure I used the right filename since exercise-data333 is the only .txt file I got.
2. if I wrote A=table2array ('T'), Matlab returns en error message(as I wrote last night):Error: Cell contents reference from a non cell array object Error in table2array (line 27) a = t{:,:};
Totally lost now...

Sign in to comment.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!