Community
cancel
Showing results for 
Search instead for 
Did you mean: 
bcor
Beginner
122 Views

MATLAB element-wise product

hi,

first, i am a newbie here, therefore sorry for any inconvenient or inappropriate content of this message.

i am trying to convert some matlab+simulink program to c++ and searching for the functions coming with MATLAB in MKL.

my project uses matrices and i couldnt find how to convert the following expression from matlab:

"A=rand(10);
B=rand(10);
C= A.*B;"

is this a special matlab function or is there an implementation for this in mkl?

i have the same problem also with the function/block in simulink; "weighted moving average"

i searched the documents and the forum, but couldnt find information.

would be pleased to solve

thanks in advance
0 Kudos
4 Replies
mecej4
Black Belt
122 Views

At the outset you should recognize that Matlab is an interpreted language with an emphasis on matrix manipulation, whereas Fortran (in which Lapack was written) and C++ are compiled languages. Compiled languages have far fewer verbs (or primitives, if you prefer) than interpreted languages. Any urge to wish that one were more like the other is, therefore, misguided unless one already has much experience with the former.

Fortran is endowed with few standard procedures for mathematical tasks, but has the features needed to build such procedures into, e.g., a library such as MKL. You will have to look in the MKL documentation MKL and find routines which when assembled together will fulfil your needs.

Matlab is proprietary, whereas C and Fortran are covered by international standards.
TimP
Black Belt
122 Views

As mecej4 implies, Fortran has built-in array operations which may resemble matlab, but C++ itself does not. There are C++ class implementations which address this, but you have the (big) initial step of deciding whether you like one or more of those:
blitz++ (open source)
ArBB (proprietary)
C interface to MKL Fortran style functions (both open source and proprietary optimized versions)
.....
ArturGuzik
Valued Contributor I
122 Views

Hi,

apart from options mentioned so far, you can also try to use Python (numpy lib) and its implementation which uses MKL for high efficiency (not quite sure why you convert the code, speed? avoiding Matlab?). Everything depends on your experience and how big the code is.
And for the record:

A=rand(10);
B=rand(10);

rand(n) returns an n-by-n matrix containing pseudorandom values drawn from the standard uniform distribution on the open interval (0,1) (this from Matlab docs) -> so you can easily find counterpart in MKL.

C= A.*B is element wise (is the element-by-element) product of the arrays A and B.

As far as "weighted moving average" routine goes. The easiest way is just to open matlab *.m or Octave file containing the code and see how they implement it (at the end of the day is just multiplication and summation).

A.
bcor
Beginner
122 Views

okay, thanks for replies

i created a project using matlab+simulink, and now the second part of it is to write it on C++ using ms visual studio 2008. Therefore, although there is nothing to do with speed or convenience, i just have to convert it.

Beside, doing this i want to use already validaded and verified libraries. Thats why i hesitate implementing the functions on my own.

There are some other libraries (blitz, lapack++ etc..) which can use MKL behind, and have the the same syntax with matlab. At first i didnt want to use many different libraries, make it complex and had questions about the performance. But now i think i should use them.

i have also heard about armadillo and boost. Maybe i should test them all initially then choose the best performance+convenient syntax.
Reply