我试图根据某些条件改变(乘以一个int)3个不同矩阵中某个位置(i,j)的值。然而,当我打印整个矩阵时,似乎它将第一个矩阵乘以所有的整数(即使是第二个和第三个矩阵的那个。
这里我声明全局矩阵和属性,包括一些内存分配:
int HEIGHT;
int WIDTH;
int ** matrixR= new int*[HEIGHT];
int ** matrixG= new int*[HEIGHT];
int ** matrixB= new int*[HEIGHT];
然后,在函数中我完成矩阵:
for(i=0; i<HEIGHT; i++){
matrixR[i]= new int[WIDTH];
matrixG[i]= new int[WIDTH];
matrixB[i]= new int[WIDTH];
}
以下是我为每个矩阵评估的条件:
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
float suma = pow(i - centerY, 2) + pow(j - centerX, 2);
if (suma > pow(radius, 2)) {
matrixR[i][j] = matrixR[i][j] * 1;
}
}
}
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
float suma = pow(i - centerY, 2) + pow(j - centerX, 2);
if (suma > pow(radius, 2)) {
matrixG[i][j] = matrixG[i][j] * 2;
}
}
}
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
float suma = pow(i - centerY, 2) + pow(j - centerX, 2);
if (suma > pow(radius, 2)) {
matrixB[i][j] = matrixB[i][j] * 3;
}
}
}
然后当我打印第一个矩阵的元素时:
for (int k = 0; k < HEIGHT; ++k) {
for (int i = 0; i < WIDTH; ++i) {
cout << matrixR[k][i] << " | ";
}
cout << '\n';
}
我应该获得matrixR[i][j]*1
我得到matrixB[i][j]
。矩阵是否会互相覆盖?有什么不对吗?
答案 0 :(得分:1)
我添加了centerX,centerY和radius的值,并添加了一个初始化部分(所有矩阵元素都是后面的一个)。
我找不到错误,输出是
1 | 1 | 1 | 1 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 |
1 | 1 | 1 | 1 | 1 | 1 | 1 |
2 | 2 | 2 | 2 | 2 | 2 | 2 |
2 | 2 | 2 | 2 | 2 | 2 | 2 |
2 | 2 | 2 | 1 | 2 | 2 | 2 |
2 | 2 | 1 | 1 | 1 | 2 | 2 |
2 | 2 | 2 | 1 | 2 | 2 | 2 |
3 | 3 | 3 | 3 | 3 | 3 | 3 |
3 | 3 | 3 | 3 | 3 | 3 | 3 |
3 | 3 | 3 | 1 | 3 | 3 | 3 |
3 | 3 | 1 | 1 | 1 | 3 | 3 |
3 | 3 | 3 | 1 | 3 | 3 | 3 |
这是我使用过的代码:
// init variables
int HEIGHT = 5;
int WIDTH = 7;
int centerY = 3;
int centerX = 3;
int radius = 1;
int ** matrixR= new int*[HEIGHT];
int ** matrixG= new int*[HEIGHT];
int ** matrixB= new int*[HEIGHT];
for(int i=0; i<HEIGHT; i++){
matrixR[i]= new int[WIDTH];
matrixG[i]= new int[WIDTH];
matrixB[i]= new int[WIDTH];
}
// init matrix values
{
for (int k = 0; k < HEIGHT; ++k)
for (int i = 0; i < WIDTH; ++i)
matrixR[k][i] = 1;
for (int k = 0; k < HEIGHT; ++k)
for (int i = 0; i < WIDTH; ++i)
matrixG[k][i] = 1;
for (int k = 0; k < HEIGHT; ++k)
for (int i = 0; i < WIDTH; ++i)
matrixB[k][i] = 1;
}
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < HEIGHT; ++j) {
float suma = pow(static_cast< double >(i) - centerY, 2) + pow( static_cast< double >( j ) - centerX, 2);
if (suma > pow( static_cast< double >( radius ), 2)) {
matrixR[i][j] = matrixR[i][j] * 1;
}
}
}
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
float suma = pow( static_cast< double >( i ) - centerY, 2) + pow( static_cast< double >( j ) - centerX, 2);
if (suma > pow( static_cast< double >( radius ), 2)) {
matrixG[i][j] = matrixG[i][j] * 2;
}
}
}
for (int i = 0; i < HEIGHT; ++i) {
for (int j = 0; j < WIDTH; ++j) {
float suma = pow( static_cast< double >( i ) - centerY, 2) + pow( static_cast< double >( j ) - centerX, 2);
if (suma > pow( static_cast< double >( radius ), 2)) {
matrixB[i][j] = matrixB[i][j] * 3;
}
}
}
for (int k = 0; k < HEIGHT; ++k) {
for (int i = 0; i < WIDTH; ++i) {
cout << matrixR[k][i] << " | ";
}
cout << '\n';
}
cout << '\n';
cout << '\n';
for (int k = 0; k < HEIGHT; ++k) {
for (int i = 0; i < WIDTH; ++i) {
cout << matrixG[k][i] << " | ";
}
cout << '\n';
}
cout << '\n';
cout << '\n';
for (int k = 0; k < HEIGHT; ++k) {
for (int i = 0; i < WIDTH; ++i) {
cout << matrixB[k][i] << " | ";
}
cout << '\n';
}
std::cin.get();
答案 1 :(得分:0)
添加了这个以使其编译并运行:
public function cmsField($observer)
{
// $model = Mage::registry('cms_page');
$form = $observer->getEvent()->getForm();
$fieldset = $form->addFieldset('example_metainfo_fieldset', array('legend'=>Mage::helper('cms')->__('Meta Info'),'class'=>'fieldset-wide'));
//add new field
$fieldset->addField('metatitle', 'text', array(
'name' => 'metatitle',
'label' => Mage::helper('cms')->__('Metatitle'),
'title' => Mage::helper('cms')->__('Metatitle'),
'disabled' => false,
//set field value
// 'value' => $model->getMetatitle()
));
$form->setValues(Mage::registry('cms_page')->getData());
}
输出我看起来很好......