Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
6590 Discussions

How do we select the column of a matrix?

Beginner
158 Views

Hello!

I am new to MKL and to C in general. Lets say that we have two matrices A, B of size (n,k). We would like for j =1 until k to subtrack the j column of A from B. In Matlab language the code is: for j=1:k, B(:,j)-A(:,j), end and in R language for(j=1 in k), { B[,j]-A[,j] }. I would like to ask if there is a routine to select the j column (or row) of a matrix? I searched but I had no luck.

Thank you very much.

1 Solution
Employee
158 Views

Hi GF,

What is the reason of you change the matlab or R to C? and What is your problem size (matrix)?

You may have known, you can use R or matlab directly, and get MKL performance benifit at the same time,  because MKL can be intergarted into  R and Matlab, please see the article  http://software.intel.com/en-us/articles/using-intel-mkl-with-r

So first, i may suggest you to use matlab or R directy.

Secondly, MKL mainly focus on the computation and the interfaces are mainly based on some kind of standard interface, like BLAS ( matrix, vector operation). So it is not flexiable like Matlab and R.

It hasn't single routines like data operations like extract columns from a Matrix.  But these operations are embeded in the computation.

for example, If you have to convert the matlab code to C/MKL code.  You may first convert them to C code, then call MKL function.

for example, X(:, t-j) - B(:, t-j) , as mecej4 mentioned to use daxpy.

double * Ypointer=&(X[0][t-j]);

double * Xpointer=&(B[0][t-j]);

IncX= number of columns of B

IncY =  number of columns of A;

N= row of column of A;

cblas_daxpy ( N, -1.0, Xpointer, incX, Ypointer,incY);

Then same as for A(:, :, J) * Ypointer.  If A can access by C pointer with regular pattern by  (expressed by incX and lda)

Call cblas_dgemv (const CBLAS_ORDER order, const CBLAS_TRANSPOSE TransA, const MKL_INT
M, const MKL_INT N, const double alpha, const double *A, const MKL_INT lda, const
double *X, const MKL_INT incX, Ypointer, 1).

Last, if you are using Intel C Compiler,  Array notication also is helpful to convert your matlab to C code.

You can write A[:] - refers the entire array in C code.

Best Regards,

Ying

3 Replies
Black Belt
158 Views

Why do you describe the operation in a seemingly more complicated way than just "A =  A - B"? MKL contains BLAS routines for performing matrix-matrix and vector operations. In particular, look at ?axpy routines. For such a simple operation as subtraction, you may also consider using the features that the programming language itself provides, such as those provided by BOOST.

Beginner
158 Views

Thank you for your help. I am confused with this language. I have written a program in Matlab and I want to convert it to C language using the MKL.

The  part of my code is the following

for t=1:T

c1=0; c2=0;

for j=1:p

c1=c1+A(:,:,j) * ( X(:,t-j)-B(:,t-j));

end

end

I haven't known to use the library well yet.  Thanks again.

Employee
159 Views

Hi GF,

What is the reason of you change the matlab or R to C? and What is your problem size (matrix)?

You may have known, you can use R or matlab directly, and get MKL performance benifit at the same time,  because MKL can be intergarted into  R and Matlab, please see the article  http://software.intel.com/en-us/articles/using-intel-mkl-with-r

So first, i may suggest you to use matlab or R directy.

Secondly, MKL mainly focus on the computation and the interfaces are mainly based on some kind of standard interface, like BLAS ( matrix, vector operation). So it is not flexiable like Matlab and R.

It hasn't single routines like data operations like extract columns from a Matrix.  But these operations are embeded in the computation.

for example, If you have to convert the matlab code to C/MKL code.  You may first convert them to C code, then call MKL function.

for example, X(:, t-j) - B(:, t-j) , as mecej4 mentioned to use daxpy.

double * Ypointer=&(X[0][t-j]);

double * Xpointer=&(B[0][t-j]);

IncX= number of columns of B

IncY =  number of columns of A;

N= row of column of A;

cblas_daxpy ( N, -1.0, Xpointer, incX, Ypointer,incY);

Then same as for A(:, :, J) * Ypointer.  If A can access by C pointer with regular pattern by  (expressed by incX and lda)

Call cblas_dgemv (const CBLAS_ORDER order, const CBLAS_TRANSPOSE TransA, const MKL_INT
M, const MKL_INT N, const double alpha, const double *A, const MKL_INT lda, const
double *X, const MKL_INT incX, Ypointer, 1).

Last, if you are using Intel C Compiler,  Array notication also is helpful to convert your matlab to C code.