简体   繁体   中英

In need of a matrix search algorithm

I'm a hobbyist programmer in C++ and Java. I have a two dimensional boolean array(16 rows, 16 columns) that I am using for a certain project. The contents of this array are being updated periodically. Some are being changed, other elements are kept the same. As of the moment, I am just scanning each element of the array in order for me to know which of them updated their values. Is there an algorithm or an efficient way for me to do this? The language I will use is C++.

This simple answer is, there is no efficient algorithm to scan over the matrix. You can improve the performance a bit by using more compact structures, such as bit fields and a cache friendly layout, but honestly, it does not solve the underlying problem.

The problem comes from the fact that you need to look at every value in the matrix, to find the changes.

What you do next depends on how computationally intensive a false change is. If it is a low impact change, just brute force it since 256 bits is really not that much. But if one change triggers a huge computational expensive algorithm, you want to put some bookkeeping logic around it. The simple approach is to record a queue of changes while writing them and then each tick, look at what needs to be done.

There is a powerful library armadillo . Armadillo is a high quality linear algebra library (matrix maths) for the C++ language, aiming towards a good balance between speed and ease of use.

The root matrix class is Mat<type> , see supported types on armadillo .

Mat<unsigned char> should consume the same amount of memory as a matrix of boolean values.


#include <iostream>

using  arma::Mat;

int main()
{
  Mat<unsigned char> mat; // create your matrix
  mat.set_size(16,16); //Set your matrix size

  //Index starts from 0
  for (auto i=0; i< 16; i++){ 
     for(auto j=0; j<16; j++){
          mat(i,j) //search through your elements
     }
   }
  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM