Hello, everyone. I have an m-file (named exercise_spatreg3) and a .txt file with tabular data (exercise_data333) to be processed in MatLab for calculating ρ values as part of a spatial regression task. The problem is that the provided code (please, see the code below) does not seem to run correctly and error messages show up.
%*********Spatial 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
One of the most common messages is that there is a problem with parantheses in Line 94/93/etc. The error message always leads to line
d=sqrt((data333(i,1)-data333(j,1))^2+(data333(i,2)-data333(j,2))^2);
I am a new user of this software and struggle with loading data and executing calculations in this exercise. Maybe you could help me with getting this code to work as it is just a part of the bigger process and not much time is left. Thank you for any suggestions.

 採用された回答

Mathieu NOE
Mathieu NOE 2020 年 10 月 6 日

1 投票

hello
there was no big mistakes here - just may be this :
just remember that when you load ascii / txt files, you should put a "§" in the header (text) lines - that is what I did on the data333.txt file otherwise I got an error message
%x y DEM asp grad DAI2 HI VI SLI2 Yield DAI1 SLI1
1498704 6699085 109.079 27.26 0.06 0.14 173.498 0.627 0.459 24.366 34.116 11.325
1498707 6699087 108.878 34.027 0.064 0.078 177.326 0.627 0.462 24.877 29.183 12.744
also your code is waiting for a "data333" variable. You are forced to use the same name in your data file - because if you name the txt file differently , you will also change the variable name by the same way and your code will not work anymore.
there is a better way to code it - as suggested in the attachement.

2 件のコメント

Edvinas P.
Edvinas P. 2020 年 10 月 6 日
編集済み: Edvinas P. 2020 年 10 月 6 日
Hi, Mathieu. Thank you for the quick response and sharing an improved code. Is the table the final result of the code calculations? My task is to calculate the spatial regression data for different ρ [raa in code]. When the ρ that minimize the standard error of the estimate is found use a stepvise multiple regression to find the predicting variables in SPSS. How do I figure out, which raa value produced the best result in MatLab?
Mathieu NOE
Mathieu NOE 2020 年 10 月 7 日
hello
could you please clarify :
what do you want to perform ? can you better describe your input data and between which ones do you intend to build a regression model
what is the error variable you want to minimize ?
NB : there are already available matlab functions for data regression :
and more....

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by