|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
if语句的代码与求出结果不符合,请问该如何解决和修改?
x_farm有4个因子(列);4个对象(行),我想对x_farm进行因子剔除,通过计算相关系数和方差,要求两者(i,j)相关系数大于0.9,若i 的方差大于j 的方差,就剔除j 因子。我的代码如下
clear
x_farm=[167.6200 100.4584 100.1472 160.3753;171.3246 93.5399 97.7215 148.9536;159.2697 88.2294 92.2526 142.8844;177.3102 102.1516 102.4093 163.5383]
cov=var(x_farm) %计算方差
C=corrcoef(x_farm)%计算相关系数
for i=1:length(x_farm)
for j=i:length(x_farm)
if C(j,i)>=0.9 && cov(i)>cov(j)
x_farm(:,j)=[]
else
x_farm(:,j)=x_farm(:,j)
end
end
end
结果如下:
x_farm =
167.6200 100.4584 100.1472 160.3753
171.3246 93.5399 97.7215 148.9536
159.2697 88.2294 92.2526 142.8844
177.3102 102.1516 102.4093 163.5383
cov =
56.9966 41.3726 19.0307 93.5436
C =
1.0000 0.7887 0.9010 0.7617
0.7887 1.0000 0.9740 0.9961
0.9010 0.9740 1.0000 0.9565
0.7617 0.9961 0.9565 1.0000
x_farm =
167.6200 100.4584 100.1472 160.3753
171.3246 93.5399 97.7215 148.9536
159.2697 88.2294 92.2526 142.8844
177.3102 102.1516 102.4093 163.5383
x_farm =
167.6200 100.4584 100.1472 160.3753
171.3246 93.5399 97.7215 148.9536
159.2697 88.2294 92.2526 142.8844
177.3102 102.1516 102.4093 163.5383
x_farm =
167.6200 100.4584 160.3753
171.3246 93.5399 148.9536
159.2697 88.2294 142.8844
177.3102 102.1516 163.5383
??? Attempted to access x_farm(:,4); index out of bounds because size(x_farm)=[4,3].
Error in ==> test at 10
x_farm(:,j)=x_farm(:,j) |
|