
September 12th, 2011, 08:34 PM
#1
Matlab functions need conversion
Hi,
I need help in converting a few functions from matlab to VC++. Here's the code that I have done partially in C++ and some are still in MATLAB.
#include "mbed.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include "iostream"
//#include "math.h"
Norma::Norma(){}//constructor
Norma::~Norma(){}//destructor
void Norma::norma(float Dn)
{
float D;
int ni;
float Dn;
[~,ni] = sizeof(D);
if (ni == 1)
Dn = (D  min(D))./(max(D)min(D)); //convert this equation in C++
else
vmaxD = max(D); vminD = min(D);
for (int i = 1; i<= ni; i++)
{
Dn(:,i) = (D(:,i)  vminD(i))./(vmaxD(i)vminD(i));//convert this equation in C++
}
}

September 12th, 2011, 09:41 PM
#2
Re: Matlab functions need conversion
Originally Posted by stunner08
Hi,
I need help in converting a few functions from matlab to VC++. Here's the code that I have done partially in C++ and some are still in MATLAB.
Instead of hoping that someone knows MATLAB in a C++ forum, why not tell us the name of the formula you're trying to implement?
The reason why is that it may not be as easy as translating linebyline a math formula that works in MATLAB to C++. There are things to consider such as roundoff error, possible overflow/underflow, etc.
If we knew what you were trying to do, then we won't need MATLAB syntax to figure it out.
Regards,
Paul McKenzie

September 12th, 2011, 11:15 PM
#3
Re: Matlab functions need conversion
Hi,
I'm developing an API which that will be embedded to a ARM micro controller. I was have the simulation version which is in .m or Matlab file. But it seems that I have to convert it into C++ since the micro controller compiler doesn't support calling other third party libraries.
The function that I need to convert is from the equation itself which are the min(D) and max(D).

September 13th, 2011, 08:58 AM
#4
Re: Matlab functions need conversion
From what I can tell, D and Dn are actually arrays or vectors even though you've defined them as a single float here. You'll need to use a loop to sequentially do an operation on each element. However, the min() and max() functions can be replaced almost directly by std::min_element() and std::max_element(). The only difference is, those return an iterator which you will need to dereference rather than the element itself.
Precompute the min and max values outside the loop, of course, for efficiency.

September 13th, 2011, 10:01 AM
#5
Re: Matlab functions need conversion
Originally Posted by Lindley
From what I can tell, D and Dn are actually arrays or vectors even though you've defined them as a single float here. You'll need to use a loop to sequentially do an operation on each element. However, the min() and max() functions can be replaced almost directly by std::min_element() and std::max_element(). The only difference is, those return an iterator which you will need to dereference rather than the element itself.
I don't use matlab, but look at the "vminD(i)" expression; it seems D is a matrix and min(D) is a vector of min values ... perhaps, the OP should look at a ublas C++ library instead ...
BTW, note that there's also the new minmax_element to get the min and max of a sequence simultaneously ...

September 13th, 2011, 11:26 AM
#6
Re: Matlab functions need conversion
Originally Posted by stunner08
Hi,
I'm developing an API which that will be embedded to a ARM micro controller. I was have the simulation version which is in .m or Matlab file. But it seems that I have to convert it into C++ since the micro controller compiler doesn't support calling other third party libraries.
What I'm asking is for you help us out here  tell us what all those symbols mean, or at the very least, using mathematical nomenclature, what that code is doing.
For example, what is this?
This is perfectly legal syntax in C++. It declares a single float variable called D. Is this what you want, or is it something else (maybe an array)?
Then this:
Code:
[~,ni] = sizeof(D);
What is that tilde supposed to mean? And sizeof() is legal syntax in C++.
In other words, something like "I want to have an array of floating point values called D, vMin(D) means get the minimum value in an array called D,...". etc..
Regards,
Paul McKenzie
Last edited by Paul McKenzie; September 13th, 2011 at 11:30 AM.

September 13th, 2011, 12:00 PM
#7
Re: Matlab functions need conversion
Originally Posted by Paul McKenzie
Then this:
Code:
[~,ni] = sizeof(D);
What is that tilde supposed to mean? And sizeof() is legal syntax in C++.
The size() function in MATLAB returns the dimensions of a matrix. The tilde indicates that a particular returned value should be ignored rather than stored, for functions with multiple return values. So the above code (if it were using size() rather than sizeof()) would store the number of columns in the matrix D while ignoring the number or rows.
Of course, sizeof() does not do anything even remotely similar in C++, so it is almost certainly not what you want there.
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This a Codeguru.com survey!
OnDemand Webinars (sponsored)
