查看: 132|回复: 2|关注: 0

[已解决] 在一个循环中,矩阵转置

[复制链接]

新手

32 麦片

财富积分


050


37

主题

102

帖子

0

最佳答案
本帖最后由 让时间说真话 于 2020-10-12 14:22 编辑

%%

clc;
clear all;

theta=70;        %半功率角
m=-log10(2)/log10(cosd(theta));
P_LED=0.02;
nLED=60;
P_total=nLED*nLED*P_LED;
I0=0.73;
Ar=1e-4;
Ts=1;
index=1.5;
FOV=60;
R=0.53;
G_Con=(index.^2)./(sind(FOV).^2);
rho=0.8;
lx=5;
ly=5;
lz=3;
H=2.6;
h=0.85;

T1=[1,1,H];
T2=[4,1,H];
T3=[1,4,H];
T4=[4,4,H];

%%
%
x=linspace(0,5,24);
y=linspace(0,5,24);
[XR,YR]=meshgrid(x,y);

D1=sqrt((XR-T1(1,1)).^2+(YR-T1(1,2)).^2+(H-h).^2);
D2=sqrt((XR-T2(1,1)).^2+(YR-T2(1,2)).^2+(H-h).^2);
D3=sqrt((XR-T3(1,1)).^2+(YR-T3(1,2)).^2+(H-h).^2);
D4=sqrt((XR-T4(1,1)).^2+(YR-T4(1,2)).^2+(H-h).^2);


cosphi1=(H-h)./D1;
cosphi2=(H-h)./D2;
cosphi3=(H-h)./D3;
cosphi4=(H-h)./D4;


receiver_angle1=acosd(cosphi1);
H1=(m+1).*Ar.*cosphi1.^(m+1)./(2.*pi.*D1.^2);
Pr1=P_total.*H1.*Ts.*G_Con;
Pr1(find(abs(receiver_angle1)>FOV))=0;  

receiver_angle2=acosd(cosphi2);
H2=(m+1).*Ar.*cosphi2.^(m+1)./(2.*pi.*D2.^2);
Pr2=P_total.*H2.*Ts.*G_Con;
Pr2(find(abs(receiver_angle2)>FOV))=0;

receiver_angle3=acosd(cosphi3);
H3=(m+1).*Ar.*cosphi3.^(m+1)./(2.*pi.*D3.^2);
Pr3=P_total.*H3.*Ts.*G_Con;
Pr3(find(abs(receiver_angle3)>FOV))=0;

receiver_angle4=acosd(cosphi4);
H4=(m+1).*Ar.*cosphi4.^(m+1)./(2.*pi.*D4.^2);
Pr4=P_total.*H4.*Ts.*G_Con;
Pr4(find(abs(receiver_angle4)>FOV))=0;

%%

x1=linspace(0.2,4.8,10);
y1=linspace(0.2,4.8,10);
[XR1,YR1]=meshgrid(x1,y1);

d1=sqrt((XR1-T1(1,1)).^2+(YR1-T1(1,2)).^2+(H-h).^2);
d2=sqrt((XR1-T2(1,1)).^2+(YR1-T2(1,2)).^2+(H-h).^2);
d3=sqrt((XR1-T3(1,1)).^2+(YR1-T3(1,2)).^2+(H-h).^2);
d4=sqrt((XR1-T4(1,1)).^2+(YR1-T4(1,2)).^2+(H-h).^2);

cosphip1=(H-h)./d1;
cosphip2=(H-h)./d2;
cosphip3=(H-h)./d3;
cosphip4=(H-h)./d4;

receiver_angle1=acosd(cosphip1);
h1=(m+1).*Ar.*cosphip1.^(m+1)./(2.*pi.*d1.^2);
Prp1=P_total.*h1.*Ts.*G_Con;
Prp1(find(abs(receiver_angle1)>FOV))=0;  

receiver_angle2=acosd(cosphip2);
h2=(m+1).*Ar.*cosphip2.^(m+1)./(2.*pi.*d2.^2);
Prp2=P_total.*h2.*Ts.*G_Con;
Prp2(find(abs(receiver_angle2)>FOV))=0;

receiver_angle3=acosd(cosphip3);
h3=(m+1).*Ar.*cosphip3.^(m+1)./(2.*pi.*d3.^2);
Prp3=P_total.*h3.*Ts.*G_Con;
Prp3(find(abs(receiver_angle3)>FOV))=0;

receiver_angle4=acosd(cosphip4);
h4=(m+1).*Ar.*cosphip4.^(m+1)./(2.*pi.*d4.^2);
Prp4=P_total.*h4.*Ts.*G_Con;
Prp4(find(abs(receiver_angle4)>FOV))=0;

%%

Prp_1=[Prp1(1,:),Prp1(2,:),Prp1(3,:),Prp1(4,:),Prp1(5,:),Prp1(6,:),Prp1(7,:),Prp1(8,:),Prp1(9,:),Prp1(10,:)];
Prp_2=[Prp2(1,:),Prp2(2,:),Prp2(3,:),Prp2(4,:),Prp2(5,:),Prp2(6,:),Prp2(7,:),Prp2(8,:),Prp2(9,:),Prp2(10,:)];
Prp_3=[Prp3(1,:),Prp3(2,:),Prp3(3,:),Prp3(4,:),Prp3(5,:),Prp3(6,:),Prp3(7,:),Prp3(8,:),Prp3(9,:),Prp3(10,:)];
Prp_4=[Prp4(1,:),Prp4(2,:),Prp4(3,:),Prp4(4,:),Prp4(5,:),Prp4(6,:),Prp4(7,:),Prp4(8,:),Prp4(9,:),Prp4(10,:)];



for i=1:100
for n=1:24
    for m=1:24

        d(n,m)=sqrt((Prp_1(i)-Pr1(n,m)).^2+(Prp_2(i)-Pr2(n,m)).^2+(Prp_3(i)-Pr3(n,m)).^2+(Prp_4(i)-Pr4(n,m)).^2);
    end
end


data=zeros(576,2);
data(:,1)=[XR(1,:),XR(2,:),XR(3,:),XR(4,:),XR(5,:),XR(6,:),XR(7,:),XR(8,:),XR(9,:),XR(10,:),XR(11,:),XR(12,:),XR(13,:),XR(14,:),XR(15,:),XR(16,:),XR(17,:),XR(18,:),XR(19,:),XR(20,:),XR(21,:),XR(22,:),XR(23,:),XR(24,:)];                                   
data(:,2)=[YR(1,:),YR(2,:),YR(3,:),YR(4,:),YR(5,:),YR(6,:),YR(7,:),YR(8,:),YR(9,:),YR(10,:),YR(11,:),YR(12,:),YR(13,:),YR(14,:),YR(15,:),YR(16,:),YR(17,:),YR(18,:),YR(19,:),YR(20,:),YR(21,:),YR(22,:),YR(23,:),YR(24,:)];

data1=zeros(100,2);
data1(:,1)=[XR1(1,:),XR1(2,:),XR1(3,:),XR1(4,:),XR1(5,:),XR1(6,:),XR1(7,:),XR1(8,:),XR1(9,:),XR1(10,:)];
data1(:,2)=[YR1(1,:),YR1(2,:),YR1(3,:),YR1(4,:),YR1(5,:),YR1(6,:),YR1(7,:),YR1(8,:),YR1(9,:),YR1(10,:)];

% dd=zeros(100,576);
dd(i,:)=[d(1,:),d(2,:),d(3,:),d(4,:),d(5,:),d(6,:),d(7,:),d(8,:),d(9,:),d(10,:),d(11,:),d(12,:),d(13,:),d(14,:),d(15,:),d(16,:),d(17,:),d(18,:),d(19,:),d(20,:),d(21,:),d(22,:),d(23,:),d(24,:)];

end

[C,I]=sort(dd,2);

%%

for i=1:100
    est1(i,:)=data(I(i,1),1:2);
    RMSE1(i,1)=norm(est1(i,:)-data1(i,:));
end
ave1=mean(RMSE1(:));
max1=max(RMSE1(:));
min1=min(RMSE1(:));


%%

    for i=1:100
         K=5;
    match_result=data(I(i,(1:K)),1:2);%矩阵的行1-k,
    est2(i,:)=mean(match_result);%得到knn
    RMSE2(i,1)=norm(est2(i,:)-data1(i,:));%估计值与实际值的误差
    ave2=mean(RMSE2(:));
    max2=max(RMSE2(:));
    min2=min(RMSE2(:));
    %%

      weight(i,:)=1./C(i,(1:K));%欧氏距离加权
      weight(i,:)=((weight(i,:)).')./sum(weight(i,:),2);%归一化处理
end

回复主题 已获打赏: 0 积分

举报

新手

32 麦片

财富积分


050


37

主题

102

帖子

0

最佳答案
 楼主| 发表于 2020-10-12 14:24:23 | 显示全部楼层
最后一行的weight为什么转置不了?原来是100*5的,我需要的是5*100的矩阵
回复此楼 已获打赏: 0 积分

举报

论坛优秀回答者

入门

249 麦片

财富积分


50500


3

主题

395

帖子

52

最佳答案
  • 关注者: 15
发表于 2020-10-12 15:51:34 | 显示全部楼层 |此回复为最佳答案
让时间说真话 发表于 2020-10-12 14:24
最后一行的weight为什么转置不了?原来是100*5的,我需要的是5*100的矩阵

你的weight根本没有初始化,你可以这么改:
%%

clc;
clear all;

theta=70;        %半功率角
m=-log10(2)/log10(cosd(theta));
P_LED=0.02;
nLED=60;
P_total=nLED*nLED*P_LED;
I0=0.73;
Ar=1e-4;
Ts=1;
index=1.5;
FOV=60;
R=0.53;
G_Con=(index.^2)./(sind(FOV).^2);
rho=0.8;
lx=5;
ly=5;
lz=3;
H=2.6;
h=0.85;

T1=[1,1,H];
T2=[4,1,H];
T3=[1,4,H];
T4=[4,4,H];

%%
%
x=linspace(0,5,24);
y=linspace(0,5,24);
[XR,YR]=meshgrid(x,y);

D1=sqrt((XR-T1(1,1)).^2+(YR-T1(1,2)).^2+(H-h).^2);
D2=sqrt((XR-T2(1,1)).^2+(YR-T2(1,2)).^2+(H-h).^2);
D3=sqrt((XR-T3(1,1)).^2+(YR-T3(1,2)).^2+(H-h).^2);
D4=sqrt((XR-T4(1,1)).^2+(YR-T4(1,2)).^2+(H-h).^2);


cosphi1=(H-h)./D1;
cosphi2=(H-h)./D2;
cosphi3=(H-h)./D3;
cosphi4=(H-h)./D4;


receiver_angle1=acosd(cosphi1);
H1=(m+1).*Ar.*cosphi1.^(m+1)./(2.*pi.*D1.^2);
Pr1=P_total.*H1.*Ts.*G_Con;
Pr1(find(abs(receiver_angle1)>FOV))=0;  

receiver_angle2=acosd(cosphi2);
H2=(m+1).*Ar.*cosphi2.^(m+1)./(2.*pi.*D2.^2);
Pr2=P_total.*H2.*Ts.*G_Con;
Pr2(find(abs(receiver_angle2)>FOV))=0;

receiver_angle3=acosd(cosphi3);
H3=(m+1).*Ar.*cosphi3.^(m+1)./(2.*pi.*D3.^2);
Pr3=P_total.*H3.*Ts.*G_Con;
Pr3(find(abs(receiver_angle3)>FOV))=0;

receiver_angle4=acosd(cosphi4);
H4=(m+1).*Ar.*cosphi4.^(m+1)./(2.*pi.*D4.^2);
Pr4=P_total.*H4.*Ts.*G_Con;
Pr4(find(abs(receiver_angle4)>FOV))=0;

%%

x1=linspace(0.2,4.8,10);
y1=linspace(0.2,4.8,10);
[XR1,YR1]=meshgrid(x1,y1);

d1=sqrt((XR1-T1(1,1)).^2+(YR1-T1(1,2)).^2+(H-h).^2);
d2=sqrt((XR1-T2(1,1)).^2+(YR1-T2(1,2)).^2+(H-h).^2);
d3=sqrt((XR1-T3(1,1)).^2+(YR1-T3(1,2)).^2+(H-h).^2);
d4=sqrt((XR1-T4(1,1)).^2+(YR1-T4(1,2)).^2+(H-h).^2);

cosphip1=(H-h)./d1;
cosphip2=(H-h)./d2;
cosphip3=(H-h)./d3;
cosphip4=(H-h)./d4;

receiver_angle1=acosd(cosphip1);
h1=(m+1).*Ar.*cosphip1.^(m+1)./(2.*pi.*d1.^2);
Prp1=P_total.*h1.*Ts.*G_Con;
Prp1(find(abs(receiver_angle1)>FOV))=0;  

receiver_angle2=acosd(cosphip2);
h2=(m+1).*Ar.*cosphip2.^(m+1)./(2.*pi.*d2.^2);
Prp2=P_total.*h2.*Ts.*G_Con;
Prp2(find(abs(receiver_angle2)>FOV))=0;

receiver_angle3=acosd(cosphip3);
h3=(m+1).*Ar.*cosphip3.^(m+1)./(2.*pi.*d3.^2);
Prp3=P_total.*h3.*Ts.*G_Con;
Prp3(find(abs(receiver_angle3)>FOV))=0;

receiver_angle4=acosd(cosphip4);
h4=(m+1).*Ar.*cosphip4.^(m+1)./(2.*pi.*d4.^2);
Prp4=P_total.*h4.*Ts.*G_Con;
Prp4(find(abs(receiver_angle4)>FOV))=0;

%%

Prp_1=[Prp1(1,:),Prp1(2,:),Prp1(3,:),Prp1(4,:),Prp1(5,:),Prp1(6,:),Prp1(7,:),Prp1(8,:),Prp1(9,:),Prp1(10,:)];
Prp_2=[Prp2(1,:),Prp2(2,:),Prp2(3,:),Prp2(4,:),Prp2(5,:),Prp2(6,:),Prp2(7,:),Prp2(8,:),Prp2(9,:),Prp2(10,:)];
Prp_3=[Prp3(1,:),Prp3(2,:),Prp3(3,:),Prp3(4,:),Prp3(5,:),Prp3(6,:),Prp3(7,:),Prp3(8,:),Prp3(9,:),Prp3(10,:)];
Prp_4=[Prp4(1,:),Prp4(2,:),Prp4(3,:),Prp4(4,:),Prp4(5,:),Prp4(6,:),Prp4(7,:),Prp4(8,:),Prp4(9,:),Prp4(10,:)];



for i=1:100
for n=1:24
    for m=1:24

        d(n,m)=sqrt((Prp_1(i)-Pr1(n,m)).^2+(Prp_2(i)-Pr2(n,m)).^2+(Prp_3(i)-Pr3(n,m)).^2+(Prp_4(i)-Pr4(n,m)).^2);
    end
end


data=zeros(576,2);
data(:,1)=[XR(1,:),XR(2,:),XR(3,:),XR(4,:),XR(5,:),XR(6,:),XR(7,:),XR(8,:),XR(9,:),XR(10,:),XR(11,:),XR(12,:),XR(13,:),XR(14,:),XR(15,:),XR(16,:),XR(17,:),XR(18,:),XR(19,:),XR(20,:),XR(21,:),XR(22,:),XR(23,:),XR(24,:)];                                   
data(:,2)=[YR(1,:),YR(2,:),YR(3,:),YR(4,:),YR(5,:),YR(6,:),YR(7,:),YR(8,:),YR(9,:),YR(10,:),YR(11,:),YR(12,:),YR(13,:),YR(14,:),YR(15,:),YR(16,:),YR(17,:),YR(18,:),YR(19,:),YR(20,:),YR(21,:),YR(22,:),YR(23,:),YR(24,:)];

data1=zeros(100,2);
data1(:,1)=[XR1(1,:),XR1(2,:),XR1(3,:),XR1(4,:),XR1(5,:),XR1(6,:),XR1(7,:),XR1(8,:),XR1(9,:),XR1(10,:)];
data1(:,2)=[YR1(1,:),YR1(2,:),YR1(3,:),YR1(4,:),YR1(5,:),YR1(6,:),YR1(7,:),YR1(8,:),YR1(9,:),YR1(10,:)];

% dd=zeros(100,576);
dd(i,:)=[d(1,:),d(2,:),d(3,:),d(4,:),d(5,:),d(6,:),d(7,:),d(8,:),d(9,:),d(10,:),d(11,:),d(12,:),d(13,:),d(14,:),d(15,:),d(16,:),d(17,:),d(18,:),d(19,:),d(20,:),d(21,:),d(22,:),d(23,:),d(24,:)];

end

[C,I]=sort(dd,2);

%%

for i=1:100
    est1(i,:)=data(I(i,1),1:2);
    RMSE1(i,1)=norm(est1(i,:)-data1(i,:));
end
ave1=mean(RMSE1(:));
max1=max(RMSE1(:));
min1=min(RMSE1(:));


%%
weight = zeros(100,5);

    for i=1:100
         K=5;
    match_result=data(I(i,(1:K)),1:2);%矩阵的行1-k,
    est2(i,:)=mean(match_result);%得到knn
    RMSE2(i,1)=norm(est2(i,:)-data1(i,:));%估计值与实际值的误差
    ave2=mean(RMSE2(:));
    max2=max(RMSE2(:));
    min2=min(RMSE2(:));
    %%

      weight(i,:)=1./C(i,(1:K));%欧氏距离加权
      weight(i,:)=weight(i,:)/sum(weight(i,:));%归一化处理
    end
weight = weight.';
回复此楼 已获打赏: 0 积分

举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

站长推荐上一条 /4 下一条

快速回复 返回顶部 返回列表