increment all the rows of a column in 2d array - c++

I have a 2D array:
A[65][4];
Is it possible to increment all the value of a specific column with looping through the row?
i.e. an equivalent command to:
for (int i = 0 ; i < 65 ; i++)
A[i][2]++;
Thanks

Related

mpi all_gatherv weird behaviour?

I'm trying to gather 2 matrices computed on different processes, i'm using the mpi allgatherv function to do so but I'm surprised by the results.
In the example I'm working on, the shape of the matrices is (20,80) and I would like to gather them into one matrix with (40,80) shape; with the matrix computed by process 0 stored in the 20 first rows.
Here is a sample of the code I'm using (nbRegimes is 0 in the example):
boost::mpi::communicator world;
int rank = world.rank();
std::vector< ArrayXXd> phiOutLoc(nbRegimes);
for (int iReg = 0; iReg < nbRegimes; ++iReg)
phiOutLoc[iReg].resize(nScenarioCur * nDimension, nbPoints);
... computation and storage into phiOutLoc[iReg] ...
//gathering results
ArrayXXd storeGlob(nScenarios * nDimension, nbPoints);
for (int iReg = 0; iReg < nbRegimes; ++iReg)
{
boost::mpi::all_gatherv<double>(world, phiOutLoc[iReg].data(), phiOutLoc[iReg].size(), storeGlob.data());
}
For instance, here is the beginning of the first row of phiOutLoc[iReg] computed on process 0 and process 1 :
rank 0
0 -353509 -366699 -379888 -393077 -406267 -419456 -432646 ...
rank 1
0 -399021 -413908 -428795 -443683 -458570 -473457 -488345 ...
Those rows should be stored respectively in the index 0 row of storeGlob and in the index 20 row;
if I understood the all_gatherv function behaviour correctly.
Howewer the index 0 row of storeGlob looks like this :
storeGlob row 0:
0 -366699 -393077 -419456 ... 0 -413908 -443683 -473457
and the index 20 row of storeGlob :
storeGlob row 20:
-353509 -379888 -406267 -432646 ... -399021 -428795 -458570 -488345
The index 0 row of storeGlob is filled with the even indices of the first rows of phiOutLoc[iReg] matrices.
The odd indices are stored in the index 20 row.
I can't really understand why the gathering is done that way.
Is this behaviour normal and is there a way to gather the two matrices the way I would like?

Find a value in a 2d array and move it to another 2d array

So I'm suppose to search my 2d array and find the locations (row and column number) where the search value occurred and store the row and column number in successive rows of a two dimension array
my instructions
"If the value is found in the data array, store the location (row and columns offset) where found in the data array into the locations array. Each time you find the value in the data array the location will be stored in the next row of the locations array. The row offset will be stored in column 0 and the column offset will be stored in column 1 of the locations array. Search the two dimension data array by rows."
This is where I'm confused
locations[Rsize][Rsize]=data[i][j];
Rsize++;
I don't know how to move the row and column from data array to the new locations array
locations array row 0 col 0 should have the data row
& locations array row 0 col 1 should have the data column
void find_value(short data[][15], short rows, short cols, short locations[] [2], short &Rsize, short searchValue)
{
short i,j;
Rsize=0;
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(data[i][j]==searchValue)
{
locations[Rsize][Rsize]=data[i][j];
Rsize++;
}
}
}
}
You are close. Remember, locations is just an array of arrays. You just want to append an array of size 2 to locations.
You want to change this:
locations[Rsize][Rsize]=data[i][j];
to this:
locations[Rsize][0] = i;
locations[Rsize][1] = j;

converting the index of 1D array into 2D array

How can I convert the index of 1D array into 2D array? I know how to convert a 2D array into 1D (i*the size of row+j). I want the opposite of that.
What you need to know is: How many columns should the 2D array have:
Lets say you have an array of 20 columns and 10 rows (array[20,10]):
int index = 47;
int numberOfColumns = 20;
int column = index % numberOfColumns;
int row = index / numberOfColumns;
// column == 7
// row == 2
You can just do the opposite. if n is the length of the row and x is index in 1D. You can index like
array[x/n][x%n]

How to get indexes for values in array

I need to write a function that takes as argument 2d array. Each element of array has an Integer value.
As input i have a 2D array for example:
[1][1][2][2]
[2][1][2][2]
[3][3][3][3]
[22][.......
and as output I need to store indexes for each value :
value = 1 : [0,0] ; [0,1] ; [ 1,1]
value = 2 : [1,0] ; ....
value = 3 : [2,0] ; .......
value = 22 : [.........
Size of array may be various, same as number of values.
Is it somehow possible to save that data to vector, or any other data type so later i could read those values and their indexes?
Sorry if something is unclear, Its my 1st post here:)
Cheers
Edit:
so ok what I tried to do: I created a class Indexes
class Indexes
{
public:
int x;
int y;
};
and later i created a vector vect;
and i tried to add indexes to that vector. The problem was when i tried to keep values separated for examlpe
for(int i=0 ; i<size ; i++){
for(int i=0 ; i<size ; i++){
if(array[i][j].value = 1)
Indexes ind(i,j);
vect.push_back(ind);
}
}
but all I could get is vector with only 1 values and its indexes stored;
You can store this index in a map of vectors of pairs, for example.
Here's some sample code:
typedef std::pair<int, int> ElementIndex;
typedef std::vector<ElementIndex> IndexList;
typedef std::map<int, IndexList> ValuesIndexMap;
So ValuesIndexMap is a map from value to a vector of all indices in which this value is stored in, where an index (type ElementIndex) is a pair of ints - row and column.

MFC List control and adding array to second column

I have a problem with adding an array in to a second column of list contol. The problem is, that the number of filled rows in the second column is the same as the number of filled rows in the first column. My code is :
enter code here
for (int i=0; i<velikost; i++) // velikost = 10 (velikost is size)
{
niz.Format(_T("%d"),polje[i]);
m_listCtrl.InsertItem(i,niz);
}
polje_stevcev = new int[max]; // new array with size of max number
for(int i=0; i<max + 1; i++) // lets say the max is 90
{
polje_stevcev[i] = 0;
niz2.Format(_T("%d"), polje_stevcev[i]);
m_listCtrl.SetItemText(i,1,niz2);
}`enter code here`
We see in the second for loop that the loop is performed 90 times, but in list control in in second column show me only first 10 rows, others are empty.
Picture (in second column should be 95 rows with number 0, but shows only 10) : Here is a screenshot
Thanks.