在 C++ 中创建带条件的矩阵
create a matrix with condition in C++
我想在 C++ 中从矩阵 A
创建矩阵 B
。
A
第一列是距离 D1
,第二列是距离 D2
。矩阵 B
复制 A
的相同列(和行),除非在 A
中碰巧 D2-D1=delta
超过阈值。在这种情况下,A
的行在 B
.
中分成两行
我写了一个算法,但是问题是它给出了segmentation fault
。有人可以帮忙,为什么会这样?
std::vector<float> newD1(10), newD2(10);
float box=5.;
int j=0;
for(auto i=0;i<D1.size();i++){
float delta=D2[i]-D1[i];
if (delta>box){ //break row i in two rows: j and j+1
//first half of row i goes in row j
newD1[j]=D1[i];
newD2[j]=(D1[i]+D2[i])/2.;
//second half of row i goes in j+1
D1[j+1]=(D1[i]+D2[i])/2.;
D2[j+1]=D2[i];
j=j+2; //we skip two row because we break up the original row in 2 rows
}
else{
newD1[j]=(D1[i]);
newD2[j]=D2[i];
j=j+1; //we skip one row because the original row is unchanged
}
}
这里给大家举个矩阵A
和B
的例子;我还在矩阵的每一行旁边指定 delta
。
矩阵A
:
#D1 D2 delta
|0 5 | 5
A= |5 15 | 10 }--> exceed the threshold, delta>5. Must break in 2 rows in `B`
|15 17 | 2
B
被创建为将第二行分成两行,因为 delta>5 :
#D1 D2 delta
|0 5 | 5
B= |5 10 | 5 }--> created from row #2 of `A`. `D2` is midpoint between`D1` and `D2` in row #2 in `A`
|10 15 | 5 }--> created from row #2 of `A`. `D1` is midpoint between`D1` and `D2` in row #2 in `A`
|15 17 | 2
EDIT:
如果我想递归地分解行(例如,假设在 A
、delta>3*box
中的第 2 行,这意味着我需要将该行分解为 B
).有什么建议吗?
1.You可以使用push_back来避免尺寸定义
2.You 更新了 D1 和 D2 而不是 newD1 和 newD2
#include <vector>
#include <iostream>
using namespace std;
int main()
{
std::vector<float> D1 = { 0,5,15 };
std::vector<float> D2 = { 5,15,17 };
std::vector<float> newD1, newD2;
float box = 5.;
for (auto i = 0; i < D1.size(); i++) {
float delta = D2[i] - D1[i];
if (delta > box) { //break row i in two rows: j and j+1
//first half of row i goes in row j
newD1.push_back(D1[i]);
newD2.push_back((D1[i] + D2[i]) / 2.);
//second half of row i goes in j+1
newD1.push_back ((D1[i] + D2[i]) / 2.);
newD2.push_back (D2[i]);
}
else {
newD1.push_back(D1[i]);
newD2.push_back(D2[i]);
}
}
}
我想在 C++ 中从矩阵 A
创建矩阵 B
。
A
第一列是距离 D1
,第二列是距离 D2
。矩阵 B
复制 A
的相同列(和行),除非在 A
中碰巧 D2-D1=delta
超过阈值。在这种情况下,A
的行在 B
.
我写了一个算法,但是问题是它给出了segmentation fault
。有人可以帮忙,为什么会这样?
std::vector<float> newD1(10), newD2(10);
float box=5.;
int j=0;
for(auto i=0;i<D1.size();i++){
float delta=D2[i]-D1[i];
if (delta>box){ //break row i in two rows: j and j+1
//first half of row i goes in row j
newD1[j]=D1[i];
newD2[j]=(D1[i]+D2[i])/2.;
//second half of row i goes in j+1
D1[j+1]=(D1[i]+D2[i])/2.;
D2[j+1]=D2[i];
j=j+2; //we skip two row because we break up the original row in 2 rows
}
else{
newD1[j]=(D1[i]);
newD2[j]=D2[i];
j=j+1; //we skip one row because the original row is unchanged
}
}
这里给大家举个矩阵A
和B
的例子;我还在矩阵的每一行旁边指定 delta
。
矩阵A
:
#D1 D2 delta
|0 5 | 5
A= |5 15 | 10 }--> exceed the threshold, delta>5. Must break in 2 rows in `B`
|15 17 | 2
B
被创建为将第二行分成两行,因为 delta>5 :
#D1 D2 delta
|0 5 | 5
B= |5 10 | 5 }--> created from row #2 of `A`. `D2` is midpoint between`D1` and `D2` in row #2 in `A`
|10 15 | 5 }--> created from row #2 of `A`. `D1` is midpoint between`D1` and `D2` in row #2 in `A`
|15 17 | 2
EDIT:
如果我想递归地分解行(例如,假设在 A
、delta>3*box
中的第 2 行,这意味着我需要将该行分解为 B
).有什么建议吗?
1.You可以使用push_back来避免尺寸定义
2.You 更新了 D1 和 D2 而不是 newD1 和 newD2
#include <vector>
#include <iostream>
using namespace std;
int main()
{
std::vector<float> D1 = { 0,5,15 };
std::vector<float> D2 = { 5,15,17 };
std::vector<float> newD1, newD2;
float box = 5.;
for (auto i = 0; i < D1.size(); i++) {
float delta = D2[i] - D1[i];
if (delta > box) { //break row i in two rows: j and j+1
//first half of row i goes in row j
newD1.push_back(D1[i]);
newD2.push_back((D1[i] + D2[i]) / 2.);
//second half of row i goes in j+1
newD1.push_back ((D1[i] + D2[i]) / 2.);
newD2.push_back (D2[i]);
}
else {
newD1.push_back(D1[i]);
newD2.push_back(D2[i]);
}
}
}