C++ Submatrix from a Multidimensional Vector/Matrix - c++

In MSVS C++ I have a multidimensional vector (matrix). I am not using arrays.
For example:
vector< vector<float> > image(1056, vector<float>(366));
After data is included in the vector from another source how is it possible to create a sub matrix from this matrix, given an pixel co-ordinate and the number of columns and rows needed?
For example, I have:
1 2 3 4
5 6 7 8
9 10 11 12
I want:
6 7
10 11
Seems basic but I am new to this concept. There are examples but they use arrays and I was unable to change the samples around for my own need.

There is no simple way to do it. You should create new two-dimensional array of desired size and copy pieces of data to it.
You may want to access matrix through some view, which would be proxy class, mapping view indices, to underlaying data indices

Related

building an array or matrix from stdin

I'm trying to build an array / matrix from a command given through stdin. The command is formatted like this:
nameOfArray build numberOfDimensions : dimensionList : valueList
Another example:
B build 1 : 3 : 4,5,6
The command needs to work for up to three dimensions, and I am completely stumped as to how to implement it.
Since we are limited to three dimensions, the problem is easy. We simply treat all the cases as the 3 dimensional case, with height and depth set to one for the lower dimensions.
So we set up the array with malloc() or std::vector::resize() width * height * depth, then read the values in one by one. In C, the job is done. In C++, you might then need to fiddle about to turn your vector into a multi-dimensional matrix class with a nice interface.

The Concept of Bilinear Interpolation by Accessing 2-d array like 1-d array does

In 2-d array, there are pixels of bmp files. and its size is width(3*65536) * height(3*65536) of which I scaled.
It's like this.
1 2 3 4
5 6 7 8
9 10 11 12
Between 1 and 2, There are 2 holes as I enlarged the original 2-d array. ( multiply 3 )
I use 1-d array-like access method like this.
array[y* width + x]
index
0 1 2 3 4 5 6 7 8 9...
1 2 3 4 5 6 7 8 9 10 11 12
(this array is actually 2-d array and is scaled by multiplying 3)
now I can patch the hole like this solution.
In double for loop, in the condition (j%3==1)
Image[i*width+j] = Image[i*width+(j-1)]*(1-1/3) + Image[i*width+(j+2)]*(1-2/3)
In another condition ( j%3==2 )
Image[i*width+j] = Image[i*width+(j-2)]*(1-2/3) + Image[i*width+(j+1)]*(1-1/3)
This is the way I know I could patch the holes which is so called "Bilinear Interpolation".
I want to be sure about what I know before implementing this logic into my code. Thanks for reading.
Bi linear interpolation requires either 2 linear interpolation passes (horizontal and vertical) per interpolated pixel (well, some of them only require 1), or requires up to 4 source pixels per interpolated pixel.
Between 1 and 2 there are two holes. Between 1 and 5 there are 2 holes. Between 1 and 6 there are 4 holes. Your code, as written, could only patch holes between 1 and 2, not the other holes correctly.
In addition your division is integer division, and does not do what you want.
Generally you are far better off writing a r=interpolate_between(a,b,x,y) function, that interpolates between a and b at step x out of y. Then test and fix. Now scale your image horizontally using it, and check visually you got it right (especially the edges!)
Now try using it to scale vertically only.
Now do both horizontal, then vertical.
Next, write the bilinear version, which you can test again using the linear version three times (will be within rounding error). Then try to bilinear scale the image, checking visually.
Compare with the two-linear scale. It should differ only by rounding error.
At each of these stages you'll have a single "new" operation that can go wrong, with the previous code already validated.
Writing everything at once will lead to complex bug-ridden code.

Define 2D array with C++ MATLAB API

I'm using the MATLAB/C++ API.
I'm trying to create a 2D MATLAB array from a 2D C++ array. Here's what I've tried:
double testCppArr[243][5];
// Fill the testCppArr with data
mwArray *testMatlabArr = new mwArray(243, 5, mxDOUBLE_CLASS);
testMatlabArr->SetData(testCppArr[0], 243 * 5);
And that's created for me vector with length of 243×5, instead of a matrix with 243 rows and 5 columns. I know that's the same in memory, but I want to create a matrix and not a vector.
Where is the mistake?
I've also read the MATLAB documentation, but didn't find a solution
The code in the question define a 2D MATLAB matrix.
My problem was in the matrix data read.

Erase rows from Eigen matrix

I have a Eigen::MatrixXd and a vector<int> of the indexes of that rows that I need to erase from the original matrix.
Is there a way to achieve this result as fast as possible?
Example:
Matrix:
1
2
4
0
Indexes of rows to remove {0, 2}.
Matrix:
2
0
Unfortunately, the answer is you'll have to roll your own, i.e. create a VectorXd of the size of the std::vector and fill it manually in a loop. When asked if a Matlab style conditional creation of matrices (B=A(A(1,:)<3,:)) exists, the dev (ggael) indicated that that feature would come later. I wouldn't be surprised if it's a SO style 6-8 weeks ;)

Need explanation of a matlab expression

Can someone explain the last line of this MatLab expression? I need to convert this to C++ and I do not have any experience in matlab syntax.
LUT = zeros(fix(Max - Min),1);
Bin= 1+LUT(round(Image));
Image is an input image, Min and Max are image minimum and maximum grey levels.
Is Bin going to be an array? What shall it contain? What are the dimensions, same as LUT or Image? What is the '1' stands for (add 1 to each member of array or a shift in array positions? I cannot find any example of this.
Thanks in advance.
LUT is a column vector that has a number of entries that is equal to the difference in maximum and minimum intensities in your image. LUT(round(Image)) retrieves the entries in your vector LUT which are given by the command round(Image). The dimension of Bin will be equal to the size of your matrix Image, and the entries will be equal to the corresponding indices from the LUT vector. So, say you have a 3x3 matrix Image, whose rounded values are as follows:
1 2 3
2 2 4
1 5 1
Then LUT(round(Image)) will return:
LUT(1) LUT(2) LUT(3)
LUT(2) LUT(2) LUT(4)
LUT(1) LUT(5) LUT(1)
And 1+LUT(round(Image)) will return:
1+LUT(1) 1+LUT(2) 1+LUT(3)
1+LUT(2) 1+LUT(2) 1+LUT(4)
1+LUT(1) 1+LUT(5) 1+LUT(1)
Note that this only works if all entries in round(Image) are positive, because you can't use zero/negative indexing in the LUT vector (or any MATLAB matrix/vector, for that matter).