CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2011
    Posts
    10

    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++
    }

    }

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Matlab functions need conversion

    Quote Originally Posted by stunner08 View Post
    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 line-by-line a math formula that works in MATLAB to C++. There are things to consider such as round-off 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

  3. #3
    Join Date
    Sep 2011
    Posts
    10

    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).

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  5. #5
    Join Date
    Oct 2008
    Posts
    1,456

    Re: Matlab functions need conversion

    Quote Originally Posted by Lindley View Post
    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 ...

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Matlab functions need conversion

    Quote Originally Posted by stunner08 View Post
    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?
    Code:
    float D;
    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.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Matlab functions need conversion

    Quote Originally Posted by Paul McKenzie View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured