Porting Matlab to C

This is a discussion on Porting Matlab to C within the C Programming forums, part of the General Programming Boards category; Hi chums, I am trying to turn a matlab script into C, and have a problem with one line: Code: ...

  1. #1
    Yes, I play CounterStrike
    Join Date
    Jun 2005
    Location
    Edinburgh, Scotland
    Posts
    33

    Porting Matlab to C

    Hi chums, I am trying to turn a matlab script into C, and have a problem with one line:
    Code:
    X = A\b
    Where A is a matrix that m~=n and b is a column matrix of proportions m,1.

    The full script I am trying to convert (and have up until that point) is:

    Code:
    %
    % gsolve.m − Solve for imaging system response function
    %
    % Given a set of pixel values observed for several pixels in several
    % images with different exposure times, this function returns the
    % imaging systemís response function g as well as the log film irradiance
    % values for the observed pixels.
    %
    % Assumes:
    %
    % Zmin = 0
    % Zmax = 255
    %
    % Arguments:
    %
    % Z(i,j) is the pixel values of pixel location number i in image j
    % B(j) is the log delta t, or log shutter speed, for image j
    % l is lamdba, the constant that determines the amount of smoothness
    % w(z) is the weighting function value for pixel value z
    %
    % Returns:
    %
    % g(z) is the log exposure corresponding to pixel value z
    % lE(i) is the log film irradiance at pixel location i
    %
    
    function [g,lE]=gsolve(Z,B,l,w)
    
    n = 256;
    A = zeros(size(Z,1)*size(Z,2)+n+1,n+size(Z,1));
    b = zeros(size(A,1),1);
    
    %% Include the data−fitting equations
    
    k = 1;
    for i=1:size(Z,1)
        for j=1:size(Z,2)
            wij = w(Z(i,j)+1);
            A(k,Z(i,j)+1) = wij; A(k,n+i) = −wij; b(k,1) = wij * B(i,j);
            k=k+1;
        end
    end
    
    %% Fix the curve by setting its middle value to 0
    A(k,129) = 1;
    k=k+1;
    
    %% Include the smoothness equations
    for i=1:n−2
        A(k,i)=l*w(i+1); A(k,i+1)=−2*l*w(i+1); A(k,i+2)=l*w(i+1);
        k=k+1;
    end
    
    %% Solve the system using SVD
    x = A\b;
    
    
    g = x(1:n);
    lE = x(n+1:size(x,1));
    It is used to generate the response function, using SVD (single value decomposition), of a digital image process by finding the pixel value difference over a set number of known exposure images.

    Matlab help defines '\' as:

    Code:
    \	Backslash or matrix left division. If A is a square matrix, A\B is
    roughly the same as inv(A)*B, except it is computed in a different 
    way. If A is an n-by-n matrix and B is a column vector with n 
    components, or a matrix with several such columns, then X = A\B is 
    the solution to the equation AX = B computed by Gaussian 
    elimination. A warning message is displayed if A is badly scaled or 
    nearly singular. See the reference page for mldivide for more 
    information.
    
    	If A is an m-by-n matrix with m ~= n and B is a column vector 
    with m components, or a matrix with several such columns, then X =
     A\B is the solution in the least squares sense to the under- or 
    overdetermined system of equations AX = B. The effective rank, k, of
     A is determined from the QR decomposition with pivoting (see 
    Algorithm for details). A solution X is computed that has at most k 
    nonzero components per column. If k < n, this is usually not the 
    same solution as pinv(A)*B, which is the least squares solution with
     the smallest norm
    Thanks in advance for any help,
    Giant.

    p.s. If you can think of a better way of doing SVD with C, don't hesitate to tell me , I have run into a total brick wall here....
    Last edited by Giant; 06-28-2005 at 04:55 AM.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    C has no matrix functions at all, just operations on individual memory addresses. You can emulate matrix functions (after all, Matlab has to do the same thing as most hardware lacks the capability to do matrix math), which is a bit easier in C++.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert Matlab Algebraic Sol to C compatible
    By Takeshi in forum C Programming
    Replies: 3
    Last Post: 01-08-2009, 06:35 PM
  2. MATLAB importing C libraries
    By chico1st in forum C Programming
    Replies: 1
    Last Post: 05-29-2008, 03:01 PM
  3. C++ to matlab
    By loga in forum C++ Programming
    Replies: 1
    Last Post: 02-10-2008, 01:33 AM
  4. MATLAB user trying to use gcc in cygwin (with BLAS and LAPACK)
    By greek_tragedy in forum C Programming
    Replies: 0
    Last Post: 07-23-2007, 03:29 PM
  5. C/C++ and Matlab?
    By darkwalk in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2006, 04:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21