Repository navigation
Expand file tree
/
Copy patharmaMex_demo.cpp
More file actions
64 lines (48 loc) · 2.12 KB
/
Copy patharmaMex_demo.cpp
File metadata and controls
64 lines (48 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2014 Conrad Sanderson (http://conradsanderson.id.au)
// Copyright 2014 National ICT Australia (NICTA)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------------
// Demonstration of how to connect Armadillo with Matlab mex functions.
// Version 0.2
#include "armaMex.hpp"
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// Check the number of input arguments.
if (nrhs != 2)
mexErrMsgTxt("Incorrect number of input arguments.");
// Check type of input.
if ( (mxGetClassID(prhs[0]) != mxDOUBLE_CLASS) || (mxGetClassID(prhs[1]) != mxDOUBLE_CLASS) )
mexErrMsgTxt("Input must me of type double.");
// Check if input is real.
if ( (mxIsComplex(prhs[0])) || (mxIsComplex(prhs[1])) )
mexErrMsgTxt("Input must be real.");
// Create matrices X and Y from the first and second argument.
mat X = armaGetPr(prhs[0]);
mat Y = armaGetPr(prhs[1]);
// Our calculations require that matrices must be of the same size
if ( size(X) != size(Y) )
mexErrMsgTxt("Matrices should be of same size.");
// Perform calculations
mat A = X + Y;
mat B = X % Y; // % means element-wise multiplication in Armadillo
// Create cube C with A and B as slices.
cube C(A.n_rows, A.n_cols, 2);
C.slice(0) = A;
C.slice(1) = B;
// Create the output argument plhs[0] to return cube C
plhs[0] = armaCreateMxMatrix(C.n_rows, C.n_cols, C.n_slices);
// Return the cube C as plhs[0] in Matlab/Octave
armaSetCubePr(plhs[0], C);
return;
}