x = w11v1v2 + w12v2v3 + w13v3v1 + w14v1 + w15v2 + w16*v3 y = w21v1v2 + w22v2v3 + w23v3v1 + w24v1 + w25v2 + w26*v3,转换成[x,y]'等于某个矩阵与矩阵[v1,v2,v3]相乘的形式
时间: 2023-08-15 11:06:27 浏览: 137
矩阵形式为:
```
[x, y] = [v1, v2, v3] * W
```
其中,
```
W = [[w11v2 + w13v3 + w14, w12v3 + w15, w11v1 + w13v3 + w16],
[w21v2 + w23v3 + w24, w22v3 + w25, w21v1 + w23v3 + w26]]
```
请注意,这里的乘法是指矩阵乘法,即矩阵[v1, v2, v3]与矩阵W相乘。
相关问题
完善以下程序:clear all X = [ 0 0 1; 0 1 1; 1 0 1; 1 1 1; ]; D = [ 0 0 1 1 ]; E1 = zeros(1000, 1); E2 = zeros(1000, 1); W11 = 2*rand(4, 3) - 1; % Cross entropy W12 = 2*rand(1, 4) - 1; % W21 = W11; % Sum of squared error W22 = W12; % for epoch = 1:1000 [W11 W12] = BackpropCE(W11, W12, X, D); [W21 W22] = BackpropXOR(W21, W22, X, D); es1 = 0; es2 = 0; N = 4; for k = 1:N x = X(k, :)'; d = D(k); v1 = W11*x; y1 = Sigmoid(v1); v = W12*y1; y = Sigmoid(v); % 填空:es1=? v1 = W21*x; y1 = Sigmoid(v1); v = W22*y1; y = Sigmoid(v); % 填空:es2=? end E1(epoch) = es1 / N; E2(epoch) = es2 / N; end plot(E1, 'r') hold on plot(E2, 'b:') xlabel('Epoch') ylabel('Average of Training error') legend('Cross Entropy', 'Sum of Squared Error')
clear all
X = [ 0 0 1;
0 1 1;
1 0 1;
1 1 1;
];
D = [ 0
0
1
1
];
E1 = zeros(1000, 1);
E2 = zeros(1000, 1);
W11 = 2*rand(4, 3) - 1; % Cross entropy
W12 = 2*rand(1, 4) - 1; %
W21 = W11; % Sum of squared error
W22 = W12; %
for epoch = 1:1000
[W11, W12, es1] = BackpropCE(W11, W12, X, D);
[W21, W22, es2] = BackpropXOR(W21, W22, X, D);
es1 = 0;
es2 = 0;
N = 4;
for k = 1:N
x = X(k, :)';
d = D(k);
v1 = W11*x;
y1 = Sigmoid(v1);
v = W12*y1;
y = Sigmoid(v);
es1 = es1 + CrossEntropy(d, y); % 填空:es1=?
v1 = W21*x;
y1 = Sigmoid(v1);
v = W22*y1;
y = Sigmoid(v);
es2 = es2 + SSE(d, y); % 填空:es2=?
end
E1(epoch) = es1 / N;
E2(epoch) = es2 / N;
end
plot(E1, 'r')
hold on
plot(E2, 'b:')
xlabel('Epoch')
ylabel('Average of Training error')
legend('Cross Entropy', 'Sum of Squared Error')
% 交叉熵损失函数
function [loss] = CrossEntropy(d, y)
loss = -1 * (d * log(y) + (1 - d) * log(1 - y));
end
% 平方误差损失函数
function [loss] = SSE(d, y)
loss = (d - y) ^ 2;
end
% Sigmoid激活函数
function [y] = Sigmoid(x)
y = 1 ./ (1 + exp(-x));
end
% 反向传播算法(Cross Entropy版本)
function [W1, W2, e] = BackpropCE(W1, W2, X, D)
alpha = 0.9;
N = 4;
e = 0;
for k = 1:N
x = X(k, :)';
d = D(k);
v1 = W1 * x;
y1 = Sigmoid(v1);
v = W2 * y1;
y = Sigmoid(v);
e = e + CrossEntropy(d, y);
delta = d - y;
delta1 = y1 .* (1 - y1) .* (W2' * delta);
dW2 = alpha * delta * y1';
dW1 = alpha * delta1 * x';
W2 = W2 + dW2;
W1 = W1 + dW1;
end
end
% 反向传播算法(Sum of Squared Error版本)
function [W1, W2, e] = BackpropXOR(W1, W2, X, D)
alpha = 0.9;
N = 4;
e = 0;
for k = 1:N
x = X(k, :)';
d = D(k);
v1 = W1 * x;
y1 = Sigmoid(v1);
v = W2 * y1;
y = Sigmoid(v);
e = e + SSE(d, y);
delta = (d - y) .* y .* (1 - y);
delta1 = y1 .* (1 - y1) .* (W2' * delta);
dW2 = alpha * delta * y1';
dW1 = alpha * delta1 * x';
W2 = W2 + dW2;
W1 = W1 + dW1;
end
end
xiangji11=zeros(50,50,50); xiangji12=zeros(50,50,50); xiangji21=zeros(50,50,50); xiangji22=zeros(50,50,50); R=50000; f1=24;f2=24; arfa1=45pi/180;arfa2=45pi/180; beita1=0;beita2=0; pixel=0.01;dt=1/4500; %找到两个不同相机拍摄的图像序列之间的重叠部分。 % 比较两个相机拍摄的图像序列在 x 轴上的坐标,确定了起始帧和结束帧。 % 如果第一个相机的第一帧在 x 轴上的坐标小于第二个相机的第一帧在 x 轴上的坐标,则起始帧为第二个相机的第一帧;否则起始帧为第一个相机的第一帧。 % 同样地,如果第一个相机的第一帧在 x 轴上的坐标小于第二个相机的第一帧在 x 轴上的坐标,则结束帧为第一个相机的最后一帧;否则结束帧为第二个相机的最后一帧。 if(xiangji11(1,1)<xiangji21(1,1)) startf=xiangji21(1,1); else startf=xiangji11(1,1); end if(xiangji11(1,1)<xiangji21(1,1)) endf=xiangji21(1,1); else endf=xiangji11(1,1); end for i=startf:1:endf for j=1:1:50 if(xiangji11(j,1)==i) X11=xiangji11(j,2); Y11=xiangji11(j,3); w11=atan(X11pixel/f1); fai11=atan(Y11pixelcos(w11)/f1); X12=xiangji12(j,2); Y12=xiangji12(j,3); w12=atan(X12pixel/f1); fai12=atan(Y12pixelcos(w12)/f1); end end for j=1:1:50 if(xiangji21(j,1)==i) X21=xiangji21(j,2); Y21=xiangji21(j,3); w21=atan(X21pixel/f2); fai21=atan(Y21pixelcos(w21)/f2); X22=xiangji22(j,2); Y22=xiangji22(j,3); w22=atan(X22pixel/f2); fai22=atan(Y22pixelcos(w22)/f2); end end x1(i)=R.cot(w11+arfa1)./(cot(w11+arfa1)+cot(w21+arfa2)); z1(i)=R./(cot(w11+arfa11)+cot(w21+arfa21)); y1(i)=(z1tan(fai11+beita1))/(sin(w11+arfa1)); x2(i)=R.cot(w12+arfa1)./(cot(w12+arfa1)+cot(w22+arfa2)); z2(i)=R./(cot(w12+arfa1)+cot(w22+arfa2)); y2(i)=(ztan(fai12+beita1))/(sin(w12+arfa1)); x12(i)=(x1(i)+x2(i))/2; z12(i)=(z1(i)+z2(i))/2; y12(i)=(y1(i)+y2(i))/2; end改为vs代码
在使用Matlab代码时,需要注意:
1. Matlab中的pi表示圆周率,而在C++中需要使用std::acos(-1.0)表示圆周率。
2. 变量名称不能以数字开头。
3. 在C++中,数组的下标是从0开始的,而在Matlab中是从1开始的。
4. 需要包含一些头文件,如<cmath>和<iostream>等。
根据上述要求,可以将Matlab代码转换为以下C++代码:
```
#include <cmath>
#include <iostream>
using namespace std;
const int ROWS = 50;
const int COLS = 50;
const int DEPTH = 50;
double xiangji11[ROWS][COLS][DEPTH];
double xiangji12[ROWS][COLS][DEPTH];
double xiangji21[ROWS][COLS][DEPTH];
double xiangji22[ROWS][COLS][DEPTH];
double R = 50000;
double f1 = 24;
double f2 = 24;
double arfa1 = 45 * std::acos(-1.0) / 180;
double arfa2 = 45 * std::acos(-1.0) / 180;
double beita1 = 0;
double beita2 = 0;
double pixel = 0.01;
double dt = 1 / 4500;
int main()
{
double x1[DEPTH];
double y1[DEPTH];
double z1[DEPTH];
double x2[DEPTH];
double y2[DEPTH];
double z2[DEPTH];
double x12[DEPTH];
double y12[DEPTH];
double z12[DEPTH];
// 找到两个不同相机拍摄的图像序列之间的重叠部分。
// 比较两个相机拍摄的图像序列在 x 轴上的坐标,确定了起始帧和结束帧。
// 如果第一个相机的第一帧在 x 轴上的坐标小于第二个相机的第一帧在 x 轴上的坐标,则起始帧为第二个相机的第一帧;否则起始帧为第一个相机的第一帧。
// 同样地,如果第一个相机的第一帧在 x 轴上的坐标小于第二个相机的第一帧在 x 轴上的坐标,则结束帧为第一个相机的最后一帧;否则结束帧为第二个相机的最后一帧。
int startf, endf;
if (xiangji11[0][0][0] < xiangji21[0][0][0])
startf = xiangji21[0][0][0];
else
startf = xiangji11[0][0][0];
if (xiangji11[0][0][0] < xiangji21[0][0][0])
endf = xiangji21[0][0][0];
else
endf = xiangji11[0][0][0];
for (int i = startf; i <= endf; i++)
{
for (int j = 0; j < ROWS; j++)
{
if (xiangji11[j][0][0] == i)
{
double X11 = xiangji11[j][1][0];
double Y11 = xiangji11[j][2][0];
double w11 = std::atan(X11 * pixel / f1);
double fai11 = std::atan(Y11 * pixel * cos(w11) / f1);
double X12 = xiangji12[j][1][0];
double Y12 = xiangji12[j][2][0];
double w12 = std::atan(X12 * pixel / f1);
double fai12 = std::atan(Y12 * pixel * cos(w12) / f1);
}
if (xiangji21[j][0][0] == i)
{
double X21 = xiangji21[j][1][0];
double Y21 = xiangji21[j][2][0];
double w21 = std::atan(X21 * pixel / f2);
double fai21 = std::atan(Y21 * pixel * cos(w21) / f2);
double X22 = xiangji22[j][1][0];
double Y22 = xiangji22[j][2][0];
double w22 = std::atan(X22 * pixel / f2);
double fai22 = std::atan(Y22 * pixel * cos(w22) / f2);
}
}
x1[i] = R * std::cot(w11 + arfa1) / (std::cot(w11 + arfa1) + std::cot(w21 + arfa2));
z1[i] = R / (std::cot(w11 + arfa11) + std::cot(w21 + arfa21));
y1[i] = (z1[i] * std::tan(fai11 + beita1)) / (std::sin(w11 + arfa1));
x2[i] = R * std::cot(w12 + arfa1) / (std::cot(w12 + arfa1) + std::cot(w22 + arfa2));
z2[i] = R / (std::cot(w12 + arfa1) + std::cot(w22 + arfa2));
y2[i] = (z2[i] * std::tan(fai12 + beita1)) / (std::sin(w12 + arfa1));
x12[i] = (x1[i] + x2[i]) / 2;
z12[i] = (z1[i] + z2[i]) / 2;
y12[i] = (y1[i] + y2[i]) / 2;
}
return 0;
}
```
阅读全文