c - Create a matrix with elements taken by another matrix
问题描述
I have to implement a function which, given a matrix of 0s and 1s, returns a new matrix that contains the coordinates of the 1s. For example: if matrix is 3x3 and the output is:
1 0 1
0 1 1
0 0 1
New matrix will be 5x2 and the output will be:
0 0
0 2
1 1
1 2
2 2
Some advice? My method would be this:
int matrix[3][3];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (matrix[i][i] == 1){
//Code i need
}
}
}
解决方案
If matrix[i][j] == 1
what do you know about the coordinates [i,j]
? That they are the coordinates of a 1
:)
Secondly, will the input matrix always be 3x3? If not, you'll want to store the dimensions of the matrix in variables, and use theses variables for your for
loops.
推荐阅读
- c# - 建议在 Asp.Net Application_Error 处理程序中调用 HttpResponse.Redirect 时将 endResponse 设置为 False 吗?
- java - REST API调用中的所有用户都可以访问单个JWT令牌吗
- jenkins - Jenkins管道参数保存一个数组
- c# - 设置 Twin.Desired BindingException 无法将 JObject 转换为 TwinCollection
- c# - 红色问号将图像加载为带有 Firebase 存储的纹理
- javascript - 如何计算node.js中多个数组的值之和?
- javascript - 如何在 ServiceWorker 中获取自己的 URL 或主机名?
- reactjs - 从数组中删除项目并使用正确的数组长度更新子组件
- ansible - Ansible 循环获取下一个值
- angular - 与 p-table 一起使用时,primeng 下拉选项可见性问题