gpt4 book ai didi

c++ - 如何提取二维矩阵C++同一条对角线上的单元格索引

转载 作者:太空宇宙 更新时间:2023-11-04 13:15:23 24 4
gpt4 key购买 nike

如上所述,我试图仅使用 C++ 获取二维矩阵的元素矩阵有 MxN 维,可能是 N!=M , N >= MM < N (基本上尺寸可以是任何尺寸并在执行时间确定)我曾尝试使用 2 个嵌套的 for 循环来解决这个问题,但到目前为止,代码只会变得越来越复杂,并且不会产生一致的结果。

视觉辅助:

我试图让第二个 for 循环从左上角开始遍历矩阵的彩色单元格——即在每个循环中,第二个循环遍历的单元格的数量/位置不断变化,我开始考虑到 N 和 M 在编译时未知,想知道是否可以这样做。

提前感谢您抽出时间。

~编辑1:这是非方阵的元素迭代的样子(如果行多于列,则同样适用)

~编辑2:这是到目前为止的代码:(可测试!)

#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;

void func(void);

// use these variables to specify the dimensions arbitrarily
// keep in mind I need this to work with relatively big matrices
int row = 5, col = 5;
string arr[][10]= {{"0", "0", "0", "0", "0", "0", "0", "0", "0", "0" },
{"0", "1,1", "1,2", "1,3", "1,4", "1,5", "1,6", "1,7", "1,8", "1,9" },
{"0", "2,1", "2,2", "2,3", "2,4", "2,5", "2,6", "2,7", "2,8", "2,9" },
{"0", "3,1", "3,2", "3,3", "3,4", "3,5", "3,6", "3,7", "3,8", "3,9" },
{"0", "4,1", "4,2", "4,3", "4,4", "4,5", "4,6", "4,7", "4,8", "4,9" },
{"0", "5,1", "5,2", "5,3", "5,4", "5,5", "5,6", "5,7", "5,8", "5,9" },
{"0", "6,1", "6,2", "6,3", "6,4", "6,5", "6,6", "6,7", "6,8", "6,9" },
{"0", "7,1", "7,2", "7,3", "7,4", "7,5", "7,6", "7,7", "7,8", "7,9" },
{"0", "8,1", "8,2", "8,3", "8,4", "8,5", "8,6", "8,7", "8,8", "8,9" },
{"0", "9,1", "9,2", "9,3", "9,4", "9,5", "9,6", "9,7", "9,8", "9,9" } };


bool f = false, f2 = false;

int main (void)
{
func();
return 0;
}

void func(void)
{

if(row < col)
{
//remember that row > col
f = true;
}
unsigned short m_i; //mask for the counter of the outer for loop (i) - counts how many times the
unsigned short j_end = 1; //stores the max number of iterations the inner loop should do - increments accordingly
unsigned short k = 1; //stores the starting index of the inner loop - starts incrementing once (j_end == col)
cout << "row = " << row << ", col = " << col << endl;
cout << "total \"i\" loops " << (row + col -1) << endl << endl;
for (unsigned short i=1; i<=row + col -1; i++) // row + col -1 is the total number of diagonals in any matrix
{ // and also the total number of iterations we need
if( i > row) // here I implement the row > col scenario, the rest should be similar
{
m_i = row; // the mask should never go above the max row number
}else if(i == row)
{
m_i = row;
if (f = true) f2 = true; // using f2 remember that we've reached the max number for rows
}else{
m_i = i; // (i < row) so just pass i
}
for(unsigned short j=k; j<=j_end; j++){
cout<< arr[m_i][j]<<" ";
if(m_i > 1){
m_i--;
}else{
m_i = 1;
}
}
cout<<endl<< "*************" << endl;
if(j_end == col )
{
k++; // increment starting index of inner loop
}else{
j_end++; // max number for inner loop not yet achieved so increment max number
}

if(m_i == row)
{
k++;
}
} // end outer loop

} // end func

您可以使用这段代码自己测试它,输出应该是这样的:

您可以更改 row & col测试不同维度的值。到目前为止,我相信这段代码适用于方阵,但在 row != col 时就不是那么有效了。

~编辑3: func()应该像我之前所说的那样考虑性能,我希望矩阵非常大!

最佳答案

for( int manhattan_distance = 0; manhattan_distance < M + N - 1; ++manhattan_distance )
{
for( int i = 0; i <= manhattan_distance; ++i )
{
int j = manhattan_distance - i;
if( j < N && i < M )
{
...
}
}
}

关于c++ - 如何提取二维矩阵C++同一条对角线上的单元格索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com