# Revision history [back]

### Retrieve non-zero elements from a SparseMat while iterating through another

Suppose I have two SparseMats: sparse1 = { 1, 1, 0, 1 }, sparse2 = { 1, 1, 1, 1 }

I iterate through the sparse1 and simultaneously retrieve pairwise elements from sparse2 as follows:

const SparseMat *a = &sparse1;
const SparseMat *b = &sparse2;

SparseMatConstIterator_<double> it = a->begin<double>(),
it_end = a->end<double>();
for(; it != it_end; ++it)
{
double p = *it;
const cv::SparseMat::Node* anode = it.node();
double q = b->value<double>(anode->idx,(size_t*)&anode->hashval);
cout << p << " " << q << endl;
}


This gives me the following output:

1 1
1 1
1 1


Note that the 3rd element from both matrices is absent since sparse1 has a 0. Is there a way to NOT ignore the non-zero elements from the sparse2 WHILE iterating through sparse1?

Or do I have to stick to dense matrices for this scenario?

### Retrieve non-zero elements from a SparseMat while iterating through another

Suppose I have two SparseMats: sparse1 = { 1, 1, 0, 1 }, sparse2 = { 1, 1, 1, 1 }

I iterate through the sparse1 and simultaneously retrieve pairwise elements from sparse2 as follows:

const SparseMat *a = &sparse1;
const SparseMat *b = &sparse2;

SparseMatConstIterator_<double> it = a->begin<double>(),
it_end = a->end<double>();
for(; it != it_end; ++it)
{
double p = *it;
const cv::SparseMat::Node* anode = it.node();
double q = b->value<double>(anode->idx,(size_t*)&anode->hashval);
cout << p << " " << q << endl;
}


This gives me the following output:

1 1
1 1
1 1


Note that the 3rd element from both matrices is absent since sparse1 has a 0. Is there a way to NOT ignore the non-zero elements from the sparse2 WHILE iterating through sparse1?

Or do I have to stick to dense matrices for this scenario?