i want to combine two 2d array into array with points - arrays

I want to combine two 2D arrays into one Nx2 array.
For example a=[1,2,3] b=[4,5,6], and I want to make c=[(1,4),(2,5),(3,6)].
i want to do it in python, but i don't know what command should i use. any hint?

you can do that in any language you want, the algorithm will likely be the same. What you want to do is
create matrix c
read through each array
grab the point at index i in each
put a[i] into matrix row i column 0
put b[i] into matrix row i column 1
here's some pseudo code to illustrate
int [][] c;
for (int i = 0; i < a.length; i++) {
c[i][0] = a[i];
c[i][1] = b[i];
}

Related

Unable to understand the following code. How does the following code create a positive definite matrix

//Cannot understand use of this function
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
double sum = 0;
for(int k=0; k<n; k++) {
//Why is i*n+k used here?
sum += A[i*n+k]*A[j*n+k];
}
C[i*n+j] = sum;
int main() {
double *m4 = (double*)malloc(sizeof(double)*n*n);
//Why was gemm_ATA function used here?
gemm_ATA(m3, m4, n); //make a positive-definite matrix
printf("\n");
//show_matrix(m4,n);
}
I am making a project for parallelizing Cholesky method and found a useful code. In the given, project this function is used and I have no idea why is it used.
Also, can someone help me understand the code and its function used in the code given in the link:-
http://coliru.stacked-crooked.com/a/6f5750c20d456da9
The function gemm_ATA takes an input matrix A and calculates C = A^T * A, which is positive semi-definite by definition (note the semi-definiteness, depending on the properties of the input matrix).
Mathematically, calculating this matrix would be:
c_i,j = sum_k a_k,i * a_k,j
c_i,j is the entry of C in the i-th row and j-th column. The expressions i*n+k and j*n+k transform these 2D indices (row and column) to a 1D index of the underlying array.
gemm_ATA calculate AA^T and stores it in C. A^T is A but flipped over its diagonal. So A*A^T multiply each row of matrix A (call A[-,i]) with each column of the matrix A (call A^T[j,-]) which is also the row of A (A[-,j]).
Also, if you flatten an n*n 2D matrix to 1D matrix, you can access the first element of each i-th row by i*n+0. So if you want k-th column of ith row you have it with A[i*n+k].
Note that since you pass C by reference to your function, after calling the function, m4 is your positive definite matrix created from m3.

"C" i couldnt understand how i can add two matrises

I'm a begginer about c and i need help about it please help
{
int matris[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<4;j++)
{
printf("Sayi giriniz: "); scanf("%d",&matris[i][j]);
}
}
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
{
printf("%d ",matris[i][j]);
}
printf("\n");
}
}
As a beginner, you need to realize that programming is nothing more than problem-solving, well there is the bit about expressing the answer in a programming language.
Doing matrix addition -- how do you do it, how would a mathematician define it? Arnaldo has given you the answer to this, $A + B = C$ where $c_{ij} = a_{ij} + b_{ij}$. So already, this starts to set some restrictions on the two matrices that your are working with, notably they have to have the same number of rows and columns.
Representation of matrices -- ok, now that you know how to add to matrices, you need to figure out how you are going to represent a matrix in your program. Computer memory is a one-dimensional array of storage units, so we need to map our two dimensional structure onto this one-dimensional array. There are two ways of doing this. The first is row major which means that we write the first row to memory, then the second row and so on. The second is column major which means that we write the first column to memory and then the second row.
Consider the following 2x3 matrix:
| a b c |
| d e f |
in row-major form, it would be laid out in memory as:
+---+---+---+---+---+---+
| a | b | c | d | e | f |
+---+---+---+---+---+---+
and in column-major form, in would be laid out in memory as:
+---+---+---+---+---+---+
| a | d | b | e | c | f |
+---+---+---+---+---+---+
Remember that computer science is zero based, so where a mathematician would designate the first element in the first row as a_{11}, we will be using zero based indicies so we will designate it as a_{00}.
Most modern program languages use row-major form to store two dimensional arrays (or in this discussion matrices). So what you might ask? Well, because we are mapping a two dimensional array to a one dimensional array, and all we really know about the one dimensional array is its starting point in memory, we need to be able to change the pair (row, col) into a single index. You should convince yourself that the following equation is correct, assuming that nRow and nCol are the number of rows and columns in the matrix.
index = nCol * row + col
So, now write some code to add to matrices together. In pseudo-code form this would be:
A <-- read in first n-by-m matrix
B <-- read in second n-by-m matrix
C <-- initialize a n-by-m matrix to all zero elements.
for(r = 0; r < nRow; r++)
for(c = 0; c < nCol; c++)
C[r][c] = A[r][c] + B[r][c]
print C
It is an implementation detail to decide if you want to use fix sized matrices, i.e. 'A[2][2]', or potentially use a dynamically allocated matrix, i.e. 'A = malloc(nRow * nCol * sizeof(int));' (assuming we are storing integers). This will determine exactly how the addition line in the above pseudo-code would be written.
Hope this helps, and kinda show you how to approach problems like this.
Don't be afraid to ask additional questions if you get stuck on attempting the implementation.
Best of Luck,
T
I don't fully understand what your question is, but I can definately show you how to add two matricies of the same length elementwise, if that's what you're looking for.
#include <stdio>;
int main()
{
//this part is declaring the two arrays you want to add,
//and the array you want to store the result in.
int arrayA[3];
int arrayB[3];
int result[3];
//this part is just initializing the data in the array
arrayA[0] = 1;
arrayA[1] = 2;
arrayA[2] = 3;
arrayB[0] = 5;
arrayB[1] = 6;
arrayB[2] = 7;
//loops from 0 to 2, and adds the nth element of arrayA and
// arrayB to store in result
for(int n =0; n < 3; n++)
{
result[n] = arrayA[n] + arrayB[n];
}
//at this point, result is the addition of your arrays.
//You can print it, or whatever it is you wanted to do with it.
return 0;
}
There are many different things that could be 'adding matricies', but this is one of them.
However, the code you submitted looks like you're trying to store numbers from the keyboard into a 2D array. You're on the right track there, but on your first nested for loop your inner loop goes too far, so it's going outside of the array bounds. Try more liek this:
int matris[2][2];
for(int i=0;i<2;i++)
{
//you had j<4 here. That will put you in invalid memory!
for(int j=0;j<2;j++)
{
printf("Sayi giriniz: "); scanf("%d",&matris[i][j]);
}
}
//you had i<3 here. That will also put you in invalid memory.
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
printf("%d ",matris[i][j]);
}
printf("\n");
}
I hope I've addressed whatever question you were going for.
İyi şanslar!

How do I extract a vector from a column-major matrix in C?

Coming from a MATLAB background, I have often used the fancy matrix manipulation commands such as vec = matrix(:,1) for extracting, e.g., the first column of matrix as a vector.
Porting some code to C with the need to interface it with FORTRAN and MATLAB now has me store matrices in single-dimensional arrays with column-major order.
So basically, I am using the macro
#define SUB2IND_2D(s1, s2, i1, i2) (s1)*(i2) + (i1)
and the loops
for(size_t r=0; r<ROWS; ++r)
{
for(size_t c=0; c<COLS; ++c)
{
size_t index = SUB2IND_2D(ROWS,COLS,r,c);
// do something with matrix[index] here
}
}
in order to access the respective matrix. Now, my question is: How can I efficiently extract a column or row vector from matrix within this framework in C, just like I would do in MATLAB using matrix(:,1) or matrix(1,:) or similar?
let's say you want to extract a column number 2 give it a name ex_col:
int ex_col[];
for (size_t x=0; x<ROWS; x++)
{
size_t index = SUB2IND_2D(ROWS, COLS, x, 2); // fix column to 2 and extract all rows
ex_col[x] = matrix[index];
}
Now you can generalize it to a function
It's a little unclear what your trying to achieve. vec(r,c) will give you access to a specific element. Otherwise you answered your own question. vec(:,r) will extract your rows and vec(c,:) will extract your columns when you run your loop.

Delete a column from a double array

I'm stuck here. I've got a matrix of size NxN stored in a double array. Then I want to delete a given column, lets say the first column. So I created a new double array of size NxN-1 and copy the values from the first matrix to the second one, except the 1st column of course. But then I want to set the first array to be the second array. I am blanking here.
double matrix[N][N]
//fill up the matrix code here...
// remove first column of array
double newMatrix[N][N-1];
for(i = 0; i < N; i++){
for(j = 1; j < N; j++){
newMatrix[i][j-1] = matrix[i][j];
}
}
matrix = newMatrix; // how do I set this correctly? Do I need to realloc the first array?
You cannot assign arrays in C, which I assume that your compiler tells you. To do such dynamic memory management, you will need to use pointers instead of arrays. I suggest you read up on how malloc() and free() work so that you can do what you want.
Edit:
Another solution comes to mind if you are only removing columns (or rows): keep track of the number of rows and columns used in the array. Then you can remove a row or column within the original array without creating a copy first. Just move the data past the delete column (or row) to the left (or up) then decrement your size counters. (I hope this make sense. If not let me know and I'll elaborate.)
like Code-guru said malloc() and free() should help alot, but if u simply wanted to delete the last column the you wouldn't need two arrays:
double matrix[2][3] = {1,2,3,4,5,6}; //declaring a 2 by 3 matrix
for (i=0;i<2;i++) //rows
{
for (j=0;j<3-1;j++) //columns - 1
{
printf("%.1f ",matrix[i][j]); //I chose to display matrix...
}
printf("\n");
}
Instead of accessing elements from array[i][j], one might opt to access elements from array + stride_x[x] + stride_y[y]; where array is originally introduced as double matrix[N*N]; or double *matrix = malloc(sizeof(double)*N*N);.
The stride_y[x] would originally contain offsets of columns for all rows: 0 1 2 3 4 ... N-1 and stride_y[y] would contain similar offsets multiplied with original row width 0 N 2*N 3*N..
From these 1-D arrays one can more effortlessly delete or exchange complete rows and columns, which may come handy in eg. recursive implementation of determinant calculation / Gauss Jordan elimination.

Concatenate two matrices in C

I'm trying to concatenate the same matrix in C, and the only idea that crossed to my mind is addition, but it doesn't work. For example, if I have: {1,1;2,2}, my new matrix should be {1,1,1,1;2,2,2,2}. I want to double the number of rows. I Googled, but I didn't find anything.
Here is my code:
matrix2=realloc(matrix1,sizeof(int*)*(row));
int i,j;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
matrix2[i][j]=matrix1[i][j]+matrix1[i][j];
}
}
Use the psuedocode I provide below. Note that for any C before C99, you cannot instantiate arrays with int matrix[2*W][H] (if W and H are not #defines)
Given matrix1 and matrix 2 of equal W,H
make matrix3 of 2*W,H
for h to H
for i to W
matrix3[h][i] = matrix1[h][i]
matrix3[h][i+W] = matrix2[h][i]
Making the matrix will require 1 malloc per row, plus 1 malloc to store the array of row pointers.
Note how you will need 2 assignments in the loop instead of the one you had before. This is because you are setting in two places.
You sound like you have a background with higher level languages like matlab. In C the plus operator does not concatenate matrices. This will add the values in the matrices and store the new value into the new matrix.
Here we are copying the input matrix into a new matrix twice
for(int i = 0; i < m; i++){for(int j = 0; j < n;j++)
{ mat2[i][j] = mat[i][j];}}
for(int i = 0 ; i < m ; i++){for(int j = n; j < (2*n) ; j++){ mat2[i][j] = mat[i][j-n];}}

Resources