Making a long vector(coloumn Matrix) using small arrays [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make one vector(feature vector) which contains array elements.
Suppose I have an array arr1 of size nx1 in first iteration. I have to add this array elements to the CvMat matrix featureVect of size, 2*n x 1.
In next iteration I have an array arr2 of size nx1, and now I have to add this array to featureVect from row n+1 to 2*n (using a one-based index)
Suppose I have
int arr1[4] = {1, 2, 3, 4};
int arr2[4] = {5, 6, 7, 8};
CvMat *featureVect;
Now I want the result to look like this (where featureVect is a one column matrix)
featureVect = {1, 2, 3, 4, 5, 6, 7, 8};// featureVect size is 8x1;

If you're using C++ with OpenCV I would recommend the Mat class. Then,
Mat featureVect(8,1,CV_32S); //CV_32s <=> int (32-bit signed integer)
const int n = 4;
for(int i = 0; i < n; ++i)
{
featureVect.at<int>(i,0) = arr1[i];
featureVect.at<int>(i+n,0) = arr2[i];
}

Related

What is the difference between vector<vector<int>>vec1 and vector<int>vec2[100]? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
What is the difference between vector<vector<int>>vec1 and vector<int>vec2[100] in C++?
Please give me an example for each?
vector<vector<int>> vec1
is vector of vector integers, where the size of both vec1 and its rows (means vector<int>) can be resized as per the elements number increases.
for example, you can do
vec1.resize(2); // you have now two rows or two `vector<int>` in it!
vec1[0].resize(3); // resize or push back as many you want
vec1[1].resize(4);
now you have the following in the vec1
{
{0, 0, 0}, // vec1[0].
{0, 0, 0, 0}, // vec1[1]
}
Where as
vector<int> vec2[100]
is array of vector of integers, in which you can only change the size of array elements (means vec2[0], vec2[1],....), not the array (means vec2) itself. The size of the array is fixed, which is 100.
Means, you have (fixed sized) 100 of vector<int>. And you can resize them.
for example, when you do
vec2[0].resize(3);
vec2[1].resize(4);
you get the vec2 as
{
{0, 0, 0}, // vec2[0].
{0, 0, 0, 0}, // vec2[1]
{}, // vec2[3] with no elements in it!
....
....
{} // vec2[99] with no elements in it!
}
vector<vector>vec1 is Vector of Vectors and vectorvec2[100] is Array of Vectors.
Difference Between C++ Vector and Array
Advantages of vector over array in C++

How to use curly bracket initializer for a dynamically size array and new? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a float* "array" (not sure if that's the right term for it) that I'm passing to another function. Normally, I would do float *array = new float[6]; But right now I'm putting in some temporary code, so I would like to set all the values right away. Is there a way I can do something like this:
float *array = new float[] { 1, 2, 3, 4, 5, 6 };
And let it deduce the size, so I don't have to do this:
float *array = new float[6];
array[0] = 1;
array[1] = 2;
...
This syntax is only valid C++20. You have to specify the size of the array otherwise:
// error C++17, ok C++20
float *array = new float[] { 1, 2, 3, 4, 5, 6 };
// ok, any C++ version
float *array = new float[6] { 1, 2, 3, 4, 5, 6 };
Of course, the preferred way would be to use a std::vector for dynamically sized lists:
auto my_vec = std::vector<float>{1, 2, 3, 4, 5, 6};
If the size is static, always prefer std::array:
// C++17 syntax
auto my_array = std::array{1.f, 2.f, 3.f, 4.f, 5.f, 6.f};
// C++14 syntax
auto my_array = std::array<float, 6>{1, 2, 3, 4, 5, 6};
// can't push back into 'my_array'

(C++) How to obtain sub-array from an array WITHOUT nested loops? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Still a beginner to coding, but is there a way to obtain sub-array from an array without the use of nested loops i.e. more traditional methods?
Assuming you want a copy of a part of a vector, you can use a constructor that takes an interator for the beginning and the end of the new vector.
vector<int> array = {0, 1, 2, 3, 4, 5};
vector<int> subArray(array.cbegin() + 2, array.cbegin() + 4);
for (int i : subArray) {
cout << i << endl;
}
output:
2
3

sum values in a range of indices with O(1) complexity [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
We are given a integer vector(V[]) and two index values, A and B. We need to sum all the integers between V[A] and V[B].
For example,
V[] == {0, 1, 2, 0, 4, 0, 3}, A == 2, B == 6
Sum == V[3] + V[4] + V[5] == 4
Is it possible to solve with O(1) complexity?
I thought about using memory address operations, but I'm still not sure how it would work(something like: sum the next (B-A) addresses values from &V[A])
If preprocessing is allowed, this can be done with a secondary array.
Each element of the second array would contain the sum of the corresponding element and all preceeding elements. This one-time preprocessing is O(n) time and O(n) space.
For example:
int Vsum[7];
Vsum[0] = V[0];
for (int i=1; i<7; i++) {
Vsum[i] = Vsum[i-1] + V[i];
}
So with your given array, the corresponding summation array Vsum would contain:
{0, 1, 3, 3, 7, 7, 10}
Once you have this, you only need to perform 2 lookups: one for the upper bound and one for the lower bound. So after preprocessing, getting the range sum is O(1) every time it is performed.
For the example of A==2 and B==6, you would then calculate Vsum[B-1] - Vsum[A] == 7 - 3 == 4
O(1) Space: no problem. But O(n) time will always be required for "random" numbers.

Maximum subarray_problem understanding [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am researching the maximum subarray problem. It would appear that I haven't gotten the core idea. Let's say you have the following array: int arr[] ={10, 4, 2, 12, 16, 1} From what I understand the maximum subarray should be equal to 14, since the lowest and highest possible sub array is 2 (the third element) and 16 (the 5th element) right? Well, apperantly not. I implemented the linear time algorithm which I found here: http://heliang.me/wiki/index.php?title=4.1_The_maximum-subarray_problem
It's implementation in c++"
int max_sarr(int arr[], int size)
{
int max_sum = -9999;
int sum = 0;
for(int i = 0; i < size; i++)
{
sum += arr[i];
if(sum > max_sum)
max_sum = sum;
if(sum < 0)
sum = 0;
}
return sum;
}
int main()
{
int arr[] = {10, 4, 2, 12, 16, 1};
int p = max_sarr(arr, 6);
cout << p << endl;
return 0;
}
The output is 45. So... where is the mistake in my thought process ?
You misunderstand the problem. The problem is to find the contiguous subarray of the given array such that it has the highest sum of all subarrays. That is, it basically finds a first element and last element within the array which, if you summed up the elements including and between them, would give you the maximum possible value.
If all of the values in your array are positive, then the maximum subarray is always the entire array. In this case, if you add up all the elements in the array, you get 45.
Consider an array with values {-5, 10, -3, 22}. We can enumerate all of the subarrays of this:
Subarrays of length 0: {}
Subarrays of length 1: {-5} {10} {-3} {22}
Subarrays of length 2: {-5, 10} {10, -3} {-3, 22}
Subarrays of length 3: {-5, 10, -3} {10, -3, 22}
Subarrays of length 4: {-5, 10, -3, 22}
The subarray with the maximum sum is {10 -3 22}, whose sum is 29.
sftrabbit's answer is great, however I strongly recommend the The Maximum Subarray Problem section in CLRS book page 68. It is very clear and it also discusses the asymptotic complexity and the real life occurences of the problem.
In addition to this, as you might expect, an array with all positive elements, the maximum subarray of it will be the array itself.