SVD function built-in matlab convert to c++ code using matlab coder app.

I had a brief introduction about matlab coder in here.

Well, I have a interesting question.
It is also used to convert the MATLAB built-in functions to c ++ code?
example.. SVD function.

SVD(Singular Value Decomposition) is very useful function for solving linear algebra problem.
But it is difficult to find the source only pure c code, Often including a linear algebra as big library.

So, this article aims to convert SVD built in matlab function to c code and use the converted c code in Visual studio.

First, a start by calculating a fixed value of 4x2  matrix.

This is my example matrix and result.
This is an example in matlab help document.

First, let's look matlab code, it is very simple.
function [U, S, V] = MySVD(X)

[U, S, V]= svd(X);

X=[ 1 2
    3 4
    5 6
    7 8
[U, S, V] = MySVD(X);



Now, let's convert MySVD.m to c code using matlab corder.

However, options for the input X is set to 4x2 double.

An example using the code of MySVD c code in Visual Studio.
The result of visual studio is same with matlab.

MySVD C code is uploaded in github.

#include < iostream>
#include "MySVD.h"
using namespace std;

void main()
 double X[]={1, 3, 5, 7, 2, 4, 6, 8};
 double U[16]; //4x4
 double S[8]; //4x2
 double V[4]; //2x2

 //extern void MySVD(const real_T X[8], real_T U[16], real_T S[8], real_T V[4]);
 MySVD(X, U, S, V);

 cout << "U out" << endl;
 for(int i=0; i< 4; ++i) //row
  for(int j=0; j< 4; ++j) //col
   cout << U[i+j*4] << " ";
  cout << endl;
 cout << endl;

 cout << "S out" << endl;
 for(int i=0; i< 4; ++i) //row
  for(int j=0; j< 2; ++j) //col
   cout << S[i+j*4] << " ";
  cout << endl;
 cout << endl;


 cout << "V out" << endl;
 for(int i=0; i< 2; ++i) //row
  for(int j=0; j< 2; ++j) //col
   cout << V[i+j*2] << " ";
  cout << endl;
 cout << endl;


The sign is a little different
But value of U* S * V 'is as correct.
And sign is irrelevant since the opposite direction to the perpendicular direction.

Next let's study the use of nxm svd

First, let's create the code from matlab data type of NxN

:inf x :inf is nxn.
After code build, see the this c++ code

#include < iostream>
#include "MySVD.h"
#include "MySVD_emxAPI.h" //for emxArray_real_T
using namespace std;

void main()
double X[] = { 1, 3, 5, 7, 2, 4, 6, 8, 1, 2, 3, 4};
 double U[16]; //4x4
 double S[12]; //4x3
 double V[9]; //3x3

 emxArray_real_T *inputX, *outputV, *outputS, *outputU;
 inputX = emxCreateWrapper_real_T(&(X[0]), 4, 3);
 outputU = emxCreateWrapper_real_T(&(U[0]), 4, 4);
 outputS = emxCreateWrapper_real_T(&(S[0]), 4, 3);
 outputV = emxCreateWrapper_real_T(&(V[0]), 3, 3);

 //extern void MySVD(const emxArray_real_T *X, emxArray_real_T *U, emxArray_real_T *S, emxArray_real_T *V);
 MySVD(inputX, outputU, outputS, outputV);

 cout << "U out" << endl;
 for (int i = 0; i< 4; ++i) //row
  for (int j = 0; j< 4; ++j) //col
   cout << U[i + j * 4] << " ";
  cout << endl;
 cout << endl;

 cout << "S out" << endl;
 for (int i = 0; i< 4; ++i) //row
  for (int j = 0; j< 3; ++j) //col
   cout << S[i + j * 4] << " ";

  cout << endl;
 cout << endl;

 cout << "V out" << endl;
 for (int i = 0; i< 3; ++i) //row
  for (int j = 0; j< 3; ++j) //col
   cout << V[i + j * 3] << " ";

  cout << endl;
 cout << endl;


The input of dynamic size is set by "emxArray_real_T" type.
for example const emxArray_real_T *X

transfer double value to emxArray_real_T, we can use "emxCreateWrapper_real_T" function.
For this function, don't forget include "MySVD_emxAPI.h" or "xxxx_emxAPI.h".

You code like this.
inputX = emxCreateWrapper_real_T(&(X[0]), 4, 2);
outputU = emxCreateWrapper_real_T(&(U[0]), 4, 4);

double address is connected with emxArray_real_T.

Please check I have posted the source code in Github.

SVD 4x2 test
SVD 4xn test
SVD nx2 test
SVD 4x4 test

Matlab coder simple test and practical use in Visual studio

Matlab coder is the tool that convert matlab code into C/C++.
If you very glad immediately to hear this, You will sympathize the needs with me.

This post is simple test that how to use matlab coder and how to utilize in the Visual studio.
see below video, then you will be able to very easily know.

1. make matlab function

2. execute matlab coder app in matlab app tab

 3. set file of project name.

4. set input parameter data type

 5. set static  c/c++ library and check code generate.

6. in more setting,
no check support non finite numbers in speed tab for non-necessary code no generation.
,no check make option, and check language c -> c++

7. build and view report.

8. Create empty project in VS, add generated c++ file to project
   function call and test.

See this video more detail.

note!, in the video, mex option selection is wrong action, static c/c++ library selection is right.


Deep learning study - one hot encoding #4

One hot encoding.
That is the probabilistic value from softmax to change a reliable 1 or 0 values.
It has the value 1.0 for the correct class and 0 every where else.


Deep learning study - logistic classifier #3

Logistic Classifier 

The logistic classifier is similar to equation of the plane.

W is weight vector, X is input vector and y is output vector.
b is bias that to adjust boundary base.

Anyway, this form is context of logistic regression, called logists.

y is result value of vector calculation that is score, that is not probability.
So, to change a probability, use softmax function.

So, we can choose what probability is close to 1.

Softmax example code in python
scores = [3.0, 1.0, 0.2]
import numpy as np
def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    return np.exp(x) / np.sum( np.exp(x), 0 )

result is
[ 0.8360188   0.11314284  0.05083836]

Softmax plot example in python
# Plot softmax curves
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])

plt.plot(x, scores.T, linewidth=2)

plt.plot(x, softmax(scores).T, linewidth=2)

first plot is

second plot is

The second graph is softmax output value.
The value of the blue line is grows, lines of green and red is almost close to zero.

one more things,
What happen if scores are multiplied or divided by 10?
scores2 = np.array([3.0, 1.0, 0.2])
print( softmax( scores2 * 10))
print( softmax( scores2 / 10))

[ 9.99999998e-01 2.06115362e-09 6.91440009e-13]
[ 0.38842275 0.31801365 0.2935636 ]

If multiplied by the growing differences
If divided, the smaller the difference.

We can take advantage of these properties.
We'll want our classifier to not be too sure of itself in the beginning. -> divided
And then over time, it will gain confidence as it learns. -> multiply


Deep learning study - supervised classification #2

classification relatives to regression, reinforcement learning, ranking, detection.

deep learning study (introduction) #1

Deep learning lecture
(tensor flow based..)

part 1.

1. logistic classification

2. stochastic optimization

3. general data practices to train models( data & parameter tuning)

part 2. (we're going to go deeper)

1. Deep networks

2. Regularization (to train even bigger models)

part 3. ( will be a deep dive into image and convolutional models)

1. convolutional networks

part 4. (all about text and sequence in general)

1. embeddings

2. recurrent models


I need your help.. " cuda::meanShiftSegmentation error !! ", error message is "The function/feature is not implemented..."

Someone ask me this problem, but I also suffered this problem and I don't know the solution.
I need yours help.

source code is here.(opencv version is 3.0)
#include "opencv2/opencv.hpp"
#include "opencv2\cuda.hpp"
#include "opencv2\cudaimgproc.hpp"

using namespace cv;

int main(int, char)

 Mat pictureBGR;
 Mat pictureSegment;
 pictureBGR = imread("2.png"); //unos slike za pretragu

 cuda::GpuMat bgr;
 cuda::GpuMat convertedBgr;
 cuda::GpuMat dstBgr;

 cuda::cvtColor(bgr, convertedBgr, CV_BGR2BGRA); 
 TermCriteria criteria(CV_TERMCRIT_EPS, 0, 0.08);
 cuda::meanShiftSegmentation(convertedBgr, dstBgr, 20, 20, 3, criteria);

 namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
 imshow("Display Image", pictureSegment);
 return 0;

error message is here.

OpenCV Error: The function/feature is not implemented (You should explicitly call download method for cuda::GpuMat object) in getMat_, file /home/zburazin/opencv-3.0.0/modules/core/src/matrix.cpp, line 1211
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/zburazin/opencv-3.0.0/modules/core/src/matrix.cpp:1211: error: (-213) You should explicitly call download method for cuda::GpuMat object in function getMat_

In past, I use meanshift function like that.
But nothing changed with the different version of the old code.

Someone know this cause?

Thank you.