Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
6536 Discussions

## Extract row/column of a matrix and subvector form a vector. Beginner
1,005 Views

Hi! I would like to ask if there are routines for the following issues:

1) Extract a row/column of a matrix.

2) If theta is a vector extract a subvector. For example extract the elements 4 until 8 of  theta.

3) Merge 2 vectors. For example if a, b be row vectors I want to create a new vector c with the first row be the a vector and the second row will be the b vector.

I know that the above can be done using "for", but I' m interested in using routines (if there are).

1 Solution Employee
1,005 Views

Hi Anas,

Sorry, it seems i may mistake your quesiton as the one http://software.intel.com/en-us/forums/topic/506612.

But the basic idea is same

As the function provided by MKL are mainly based on some kind of standard interface, like BLAS ( matrix, vector operation). It may not flexiable like Matlab and R.  But it can accept C array and these operations you mentioned are embeded in the computation.

// Note: A can be accessed by pointer with regular pattern by incx, tranpose.etc.

#include <stdio.h>
#include <stdlib.h>
#include <mkl.h>

int main ()
{
double a = {{1, 2}, {3, 4}};
double b = {11, 12};
double res;

res =cblas_ddot(2, &a , 2, b, 1); // Compute a vector-vector dot product of the first column of the matrix and the vector b. The first column is the vector{1,3}
printf(" %0.2f\n", res);

//Compute a vector-vector dot product of the first column of the matrix and the vector b. The second column is the vector{2,4}
res =cblas_ddot(2, &a , 2, b, 1);
printf(" %0.2f\n", res);

cblas_dgemv(CblasRowMajor, CblasTrans, 2, 2, 1.0, &a, 2, b, 1, 0.0, y, 1);
printf(" %0.2f\n", y, y);

cblas_dgemv(CblasColMajor, CblasNoTrans, 2, 2, 1.0, &a, 2, b, 1, 0.0, y, 1);
printf(" %0.2f\n", y, y);

return 0;

}

Ying

return 0;
}

5 Replies Employee
1,005 Views

Hi Anas,

It seems Array Notation be a suitable tool for these operations.  Please See  http://software.intel.com/en-us/articles/about-intel-cilk-plus-and-how-to-get-started.

A[:] - refers the entire array
A[10:5] - refers A, A, A, A, A
A[1:4:2] - refers A, A, A, A
multiple dimensions: A[<lower bound>:<length>:<stride>][<lower bound>:<length>:<stride>]
A[:][:]- refers the entire 2 dimensional array

You can write code  like  ColumN= A[:],   SubVector=theta[4:4 ],  in your program.

It support C /Fortran lanaguage. and if you are using Intel C/Fortran Composer XE.

Best Regards,

Ying Beginner
1,005 Views

Thank you for your help. I tried to use the array notation with  the function ddot but it doesn' t work. Could you tell me please what is wrong?

int main ()
{
double a = {{1, 2}, {3, 4}};
double b = {11, 12};
double res;
res =cblas_ddot(2, a[:] , 1, b, 1); // Compute a vector-vector dot product of the first column of the matrix and the vector b. The first column is the vector{1,3}

printf(" %0.2f\n", res);

return 0;
} Employee
1,005 Views

MKL has functions that allow you to extract/copy elements of a vector, indexed using different methods (increments, masks, or index arrays). Check out the "VML Pack/Unpack Functions" (http://software.intel.com/en-us/node/470564).

For matrices, please note that matrices passed to MKL computation routines are stored as 1-dimensional arrays in memory. Employee
1,006 Views

Hi Anas,

Sorry, it seems i may mistake your quesiton as the one http://software.intel.com/en-us/forums/topic/506612.

But the basic idea is same

As the function provided by MKL are mainly based on some kind of standard interface, like BLAS ( matrix, vector operation). It may not flexiable like Matlab and R.  But it can accept C array and these operations you mentioned are embeded in the computation.

// Note: A can be accessed by pointer with regular pattern by incx, tranpose.etc.

#include <stdio.h>
#include <stdlib.h>
#include <mkl.h>

int main ()
{
double a = {{1, 2}, {3, 4}};
double b = {11, 12};
double res;

res =cblas_ddot(2, &a , 2, b, 1); // Compute a vector-vector dot product of the first column of the matrix and the vector b. The first column is the vector{1,3}
printf(" %0.2f\n", res);

//Compute a vector-vector dot product of the first column of the matrix and the vector b. The second column is the vector{2,4}
res =cblas_ddot(2, &a , 2, b, 1);
printf(" %0.2f\n", res);

cblas_dgemv(CblasRowMajor, CblasTrans, 2, 2, 1.0, &a, 2, b, 1, 0.0, y, 1);
printf(" %0.2f\n", y, y);

cblas_dgemv(CblasColMajor, CblasNoTrans, 2, 2, 1.0, &a, 2, b, 1, 0.0, y, 1);
printf(" %0.2f\n", y, y);

return 0;

}

Ying

return 0;
} Beginner
1,005 Views

Hi Ying! I appreciate your help, thank you very much. 