??? Index exceeds matrix dimensions.

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

답변 (1개)

Roger Stafford
Roger Stafford 2014년 9월 16일

2 개 추천

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

Yaojiayin
Yaojiayin 2014년 9월 16일
Hi Roger,
Thanks for your reply!
1, actually I forgot to say that I have trouble with loading data333.txt to MATLAB, I wrote
load data333.txt
and got error message that there's unrecognized x in ASCII file data333.txt. I then worte
load C:\where I saved all MATLAB docs&data\data333.txt
I got the same error message.
So I used T=readtable('data333.txt','HeaderLines',1,'Delimiter','\t')
to load data333, and now all data are displayed in the command window. However, I'm not sure if T command is the same as the load command.
2, I checked the size of data333, and I got ans=1 20
It means data333 have only 1 row and 20 columns. This is wrong for according to the data dispalyed in the command window (and also if check data333 in Excel) that data333 has actually 220 rows and 12 columns.
What could be the problem??????? (seriously Im daying here!!!)
Roger Stafford
Roger Stafford 2014년 9월 16일
Yes, I checked the array shown in "data_spatreg.zip" called "exercise-data333.txt" and the array shown would indeed be 220 by 12. Unfortunately, the 'data333' you have in your program apparently contains the text for the name "exercise-data333.txt" which is 1 by 20 characters. That is certainly what is causing your error message. You will have to figure out how to load the real 'data333'. I'm afraid I can't help you with that. I am not familiar with the 'readtable' function. You somehow have to get all that data which lies in 'exercise-data333.txt' into a single 220 by 12 matlab array of type double before you can hope to run your program.
Star Strider
Star Strider 2014년 9월 16일
If you can work with tables (R2013b +), table2array or table2cell might do what you want.
Yaojiayin
Yaojiayin 2014년 9월 16일
편집: Yaojiayin 2014년 9월 16일
Thanks for the reply! Data333 actually doesn't 'exist', I guess it is the name of either the matrix or table or whatever my teacher converted from exercise-data333.txt.
As I wrote above,I first created a table from exercise-data333.txt by T=readtable('data333.txt','HeaderLines',1,'Delimiter','\t')
In these way I got a table T which contains all data in .txt file.
If I understand it right, I need to convert this table to array first, so I ran table2array command. But I then got an error message: Error: Cell contents reference from a non cell array object Error in table2array (line 27) a = t{:,:};
How could I solve this problem?
Hikaru
Hikaru 2014년 9월 17일
편집: Hikaru 2014년 9월 17일
Your code
T=readtable('data333.txt','HeaderLines',1,'Delimiter','\t')
tells MATLAB to skip the first row (and MATLAB takes the next row vector as the column header), which is why you only have 219x12 matrix.
Simply use
T = readtable('exercise-data333.txt','Delimiter','\t');
and you should be able to get the correct 220x12 matrix.
Yaojiayin
Yaojiayin 2014년 9월 17일
Hi Hikaru, I used your code, and the size of T became: ans=1 1, don't know where's the problem.
And its keep telling me Error: unbalanced or misused parentheses or brackets Error in d=sqrt(('T'(i,1)-'T'(j,1))^2+('T'(i,2)-'T'(j,2))^2). But I don't think I used wrong number of parentheses....
Pierre Benoit
Pierre Benoit 2014년 9월 17일
Well for the problem with parentheses, it's because you refer to your table as 'T'(i,1) where as you only need to use T(i,1), etc.
Hikaru
Hikaru 2014년 9월 17일
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 2014년 9월 17일
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...

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2014년 9월 15일

댓글:

2014년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by