How to add sum of rows of a matrix into vector? - c++

Since the question would be a bit long, ill add that here, I also want to add a row in a vector to the Finald vector.
MatrixXf ProdA(7, 7);;
VectorXf Intd(7);
VectorXf Finald(7);
ProdA <<
7, 5, 1, 9, 11, 2, 0,
5, 2, 8, 3, 11, 3, 3,
3, 9, 0, 1, 3, 1, 7,
6, 0, 1, 9, 11, 33, 3,
3, 5, 3, 3, 4, 3, 3,
3, 9, 1, 1, 0, 1, 15,
6, 2, 6, 2, 5, 12, 3,
Intd << 4, 5, 2, 12, 4, 1, 6;
Finald << 0, 0, 0, 0, 0, 0, 0;
for (int i = 0; i < 7; i++){
Finald.row(i) += ProdA.rowwise().sum();
Finald.row(i) += Intd.row(i);
}
So far this is what I have got. Obviously I get an error if I put i in rowwise. So as an example, I want to add the first row of ProdA , and the first number of Intd into the first space in the Finald vector, and then loop through every row of ProdA and Intd, and sum them all into Finald.
Thanks in advance!

I'm not 100% certain that I correctly understand your problem, but the way I understood it, this should work:
VectorXf ones(7);
ones << 1, 1, 1, 1, 1, 1, 1;
Finald = ProdA * ones + Intd;
I'm not sure if your matrix library (which seems to be Eigen) stores vectors as row or column vectors. So you might have to use ones.transpose() instead.

Related

xtensor: Select rows with specific column values

I am playing around with xtensor and I just wanted to perform a simple operation to select rows with specific column values. Imagine I've the following array.
[
[0, 1, 1, 3, 4 ]
[0, 2, 1, 5, 6 ]
[0, 3, 1, 3, 2 ]
[0, 4, 1, 5, 7 ]
]
Now I want to select the rows where col2 and col4 has value 3. Which in this case is row 3.
[0, 3, 1, 3, 2 ]
I want to achieve similar to what this answer has achieved.
How can I achieve this in xtensor?
The way to go is to slice with the columns you need, and then look where the condition is true for all columns.
For the latter an overload for xt::all(...) is seemingly not implemented (yet!), but we can use xt::sum(..., axis) to achieve the same:
#include <xtensor/xtensor.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xio.hpp>
int main()
{
xt::xtensor<int,2> a =
{{0, 1, 1, 3, 4},
{0, 2, 1, 5, 6},
{0, 3, 1, 3, 2},
{0, 4, 1, 5, 7}};
auto test = xt::equal(xt::view(a, xt::all(), xt::keep(1, 3)), 3);
auto n = xt::sum(test, 1);
auto idx = xt::flatten_indices(xt::argwhere(xt::equal(n, 2)));
auto b = xt::view(a, xt::keep(idx), xt::all());
std::cout << b << std::endl;
return 0;
}

Question about Eigen::MatrixXd column-wise calculation

Is there anyway to apply the column-wise calculation as follows?
(each column divided by the last entry of the column)
Eigen::MatrixXd A(3,5), B(3,5);
A << 1, 4, 9, 16, 25,
2, 4, 6, 8, 10,
1, 2, 3, 4, 5;
B = (A.col) / (A.bottomerows<1>).col;
and B would be:
B = 1, 2, 3, 4, 5,
2, 2, 2, 2, 2,
1, 1, 1, 1, 1;
The functions you are looking for are .hnormalized() and .homogeneous(). Both can be applied .colwise() like this:
Eigen::MatrixXd B = A.colwise().hnormalized().colwise().homogeneous();
You can achieve the same with some .replicate() magic like this:
Eigen::MatrixXd B = A.array() / A.row(2).replicate(A.rows(),1).array();
(if A was an ArrayXXd, instead of a MatrixXd, you don't need to write the .array())

Min, Max, Avg Filters in opencv2.4.13

Is there any built-in function to apply min, max and Avg filters to an image in opencv2.4.13 ?
I'm using c++ .
As #Miki mentioned in the comments, boxfilter is a mean filter. Just set the kernel size you want and leave normalize=true (the default).
The functions erode and dilate are min and max filters, respectively. You can create the kernel using createMorphologyFilter, create your own, or use the default 3x3 as I've done. The borders are set by default to +inf for erode and -inf for dilate so they do not contribute to the result.
int main(int argc, const char * argv[]) {
char image_data[25] = {1, 3, 8, 8, 4,
4, 2, 7, 9, 9,
1, 5, 0, 5, 9,
3, 7, 5, 2, 1,
0, 4, 7, 9, 4};
cv::Mat image = cv::Mat(5, 5, CV_8U, image_data);
std::cout << "image = " << std::endl << image << std::endl;
cv::Mat avgImage;
// Perform mean filtering on image using boxfilter
cv::boxFilter(image, avgImage, -1, cv::Size(3,3));
std::cout << "avgImage = " << std::endl << avgImage << std::endl;
cv::Mat kernel; // Use the default structuring element (kernel) for erode and dilate
cv::Mat minImage;
// Perform min filtering on image using erode
cv::erode(image, minImage, kernel);
std::cout << "minImage = " << std::endl << minImage << std::endl;
cv::Mat maxImage;
// Perform max filtering on image using dilate
cv::dilate(image, maxImage, kernel);
std::cout << "maxImage = " << std::endl << maxImage << std::endl;
return 0;
}
Here are the results:
image =
[ 1, 3, 8, 8, 4;
4, 2, 7, 9, 9;
1, 5, 0, 5, 9;
3, 7, 5, 2, 1;
0, 4, 7, 9, 4]
avgImage =
[ 3, 4, 6, 8, 8;
3, 3, 5, 7, 7;
4, 4, 5, 5, 6;
4, 4, 5, 5, 5;
5, 5, 5, 4, 4]
minImage =
[ 1, 1, 2, 4, 4;
1, 0, 0, 0, 4;
1, 0, 0, 0, 1;
0, 0, 0, 0, 1;
0, 0, 2, 1, 1]
maxImage =
[ 4, 8, 9, 9, 9;
5, 8, 9, 9, 9;
7, 7, 9, 9, 9;
7, 7, 9, 9, 9;
7, 7, 9, 9, 9]

Get minimum element in constant time

Lets say I have an array A of size n, where 0 <= A[i] <= n.
Lets say I have 2 arrays Forward and Backward, size n, where:
Forward[i] = index j where
A[j] = min(A[i], A[i+1], ..., A[n-1])
and
Backward[i] = index j where
A[j] = min(A[i], A[i-1], ..., A[0])
My question is:
given A, Forward and Backward
given 2 indexes l and r
Can I discover the index k such that A[k] = min(A[l], A[l+1], ..., A[r]) in constant time?
No. Atleast not in O(1) time. A counter example is as follows. 0-based indexing is used here. Let
index = {0, 1, 2, 3, 4, 5, 6, 7, 8}
A = {1, 3, 5, 7, 9, 6, 4, 2, 0}
Forward = {8, 8, 8, 8, 8, 8, 8, 8, 8}
Backward = {0, 0, 0, 0, 0, 0, 0, 0, 8}
Now, if I ask you to get the index of the minimum value in range [3, 7], how will you do it?
Basically they will be of no use to find in the range [a, b]
if forward[a] > b and backward[b] < a.
No you cant. A counter example is:
A = {0, 4, 3, 2, 3, 4, 0}
Forward = {6, 6, 6, 6, 6, 6, 6}
Backward = {0, 0, 0, 0, 0, 0, 0}
l = 1, k = 5
ie Forward and Backward are of no use in that case and you have to search the array which is O(k-l).

Trying to simulate python combinations in C++ with next_permutation

I need to port a snippet written in Python to C++
but that snippet is using combinations from itertools in python.
The line that I'm really interested to porting over to C++ is this one:
for k in combinations(range(n-i),2*i):
range(n-i) in Python will generate a list from 0 to (n-i) - 1
Let n = 16, i = 5
print range(n-i)
outputs:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and python combinations will generate all possible combinations in that list.
e.g.
print list(combinations(range(n-i),2*i))
outputs:
[(0, 1, 2, 3, 4, 5, 6, 7, 8, 9),
(0, 1, 2, 3, 4, 5, 6, 7, 8, 10),
(0, 1, 2, 3, 4, 5, 6, 7, 9, 10),
(0, 1, 2, 3, 4, 5, 6, 8, 9, 10),
(0, 1, 2, 3, 4, 5, 7, 8, 9, 10),
(0, 1, 2, 3, 4, 6, 7, 8, 9, 10),
(0, 1, 2, 3, 5, 6, 7, 8, 9, 10),
(0, 1, 2, 4, 5, 6, 7, 8, 9, 10),
(0, 1, 3, 4, 5, 6, 7, 8, 9, 10),
(0, 2, 3, 4, 5, 6, 7, 8, 9, 10),
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
I want to generate similar output using std::vector and next_permutation from C++ but I'm still getting erroneous results. This is my current approach:
for(int j = 0; j < n-i; j++) {
temp_vector.push_back(j);
}
That snippet is equivalent to range(n-i) in Python.
But the following snippet:
do {
myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(),temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;
Is not equivalent to combinations(range(n-i),2*i)) in Python, and I've tried many variations and still haven't been able to come up with the results I'm expecting.
For example:
Let n = 16
i = 5
Python
>>> print len(list(combinations(range(n-i),2*i)))
11
C++
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector<int> temp_vector;
vector< vector<int> > myvector;
int n = 16, i = 5;
for(int j = 0; j < n - i; j++) {
temp_vector.push_back(j);
}
do {
myvector.push_back(temp_vector);
} while(next_permutation(temp_vector.begin(), temp_vector.begin()+2*i));
cout<<myvector.size()<<endl;
return 0;
}
g++ combinations.cpp
./a.out
3628800
Any guidance will be greatly appreciated! Thanks a lot!
combinations and permutations are not the same thing.
A combination is an unordered list of a subset of the items from another set. A permutation is a unique order of the items in the list.
You're generating all combinations of 10 things from a list of 11 things, so you'll get 11 results, each one missing a different one of the original 11 items.
Generating every permutation will generate every unique order of the original 11 items. Since the items in this case are all unique that means the result would be 11! lists where each contains all 11 items. You're only generating permutations from the first 10 items however, so you're getting 10! lists, none of which contain the 11th item.
You need to find an algorithm for generating combinations instead of permutations.
There's no built-in algorithm for combinations. std::next_permutation can be used as part of an algorithm to generate combinations: See Generating combinations in c++.
Here's an old draft proposal for algorithms for combinations, including code.