Showing posts with label matlab. Show all posts
Showing posts with label matlab. Show all posts

Thursday, November 18, 2010

Matlab Function in Fortran - perms - permutation

Below is the Fortran implementation of the perms algorithm found in Matlab. It creates a matrix whose rows consist of all possible permutations of the n elements of vector vecIn.

There are many useful utility functions in Matlab and occassionally an important but rare (difficult to find source in the internet) algorithm such as this permutation algorithm. Hopefully there will be more Fortran version of Matlab utility functions appearing here in the future.

The one listed below is unpolished and unoptimized and not suitable for large inputs. It is a very basic implementation taken directly from the mathematical definition. It is also tested only for a small number of cases. Corrections, suggestions and comments are welcomed.




!/*
! * perms - All Possible permutations
! * 
! *     (C)  Copyright 2010 xTechNotes.blogspot.com
! * 
! *   This program is free software: you can redistribute it and/or modify
! *   it under the terms of the GNU General Public License as published by
! *   the Free Software Foundation, either version 3 of the License, or
! *   (at your option) any later version.
! *
! *    This program is distributed in the hope that it will be useful,
! *    but WITHOUT ANY WARRANTY; without even the implied warranty of
! *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! *    GNU General Public License for more details.
! *
! *    You should have received a copy of the GNU General Public License
! *    along with this program.  If not, see .
! */ 


    RECURSIVE subroutine perms(n, nfact, vecIn, vecOut)
        IMPLICIT NONE 
        INTEGER, INTENT(IN) :: n                ! number of elements of original vector
        INTEGER, INTENT(IN) :: nfact            ! Factorial(n)
        REAL, INTENT(IN) :: vecIn(n)            ! original vector to be permuted
        REAL, INTENT(OUT):: vecOut(nfact, n)    ! Permutations of original vector


        ! local variables         
        real :: vecInTmp(n)
        integer :: ii , mfact
        real :: dtmp
        
        vecOut = 0.0
        if (n .le. 0) then 
            return
        elseif ( n .eq. 1 ) then            
            vecOut(1,1) = vecIn(1)
        elseif ( n .eq. 2 ) then
            vecOut(1, :) = vecIn(:)
            vecOut(2, :) = (/vecIn(2), vecIn(1)/)
        else        
            ! ii = 1
            call factorial(n-1, mfact)
            vecOut(1:mfact, 1) = vecIn(1)
            call perms(n-1, mfact,  vecIn(2:n), vecOut(1:mfact, 2:n))
            
            do ii = 2, n
                vecInTmp = VecIn
                vecInTmp(1) = VecIn(ii)
                vecInTmp(ii) = VecIn(1)
                vecOut((ii-1)*mfact+1 : ii*mfact, 1) = vecInTmp(1)                
                call perms(n-1, mfact, vecInTmp(2:n), vecOut( (ii-1)*mfact+1 : ii*mfact, 2:n))
            enddo 
        
        endif    
        
    end subroutine perms

Friday, November 12, 2010

Matlab Function in Fortran - conv2 - Convolution in 2D

Below is the Fortran implementation of the Convolution 2D algorithm, or conv2 as found in Matlab. There are many useful utility functions in Matlab and occassionally an important but rare (difficult to find source in the internet) algorithm such as this convolution algorithm. Hopefully there will be more Fortran version of Matlab utility functions appearing here in the future.

The one listed below is unpolished and unoptimized and not suitable for large inputs. It is a very basic implementation taken directly from the mathematical definition of discrete convolution in 2D. It is also tested only for a small number of cases. Corrections, suggestions and comments are welcomed.




!/*
! * CONV2 - Convolution in 2D
! *
! *     (C)  Copyright 2010 xTechNotes.blogspot.com
! *
! *   This program is free software: you can redistribute it and/or modify
! *   it under the terms of the GNU General Public License as published by
! *   the Free Software Foundation, either version 3 of the License, or
! *   (at your option) any later version.
! *
! *    This program is distributed in the hope that it will be useful,
! *    but WITHOUT ANY WARRANTY; without even the implied warranty of
! *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! *    GNU General Public License for more details.
! *
! *    You should have received a copy of the GNU General Public License
! *    along with this program.  If not, see .
! */  
  
    subroutine conv2(nXi, nXj, nHi, nHj, nYi, nYj, X, H, Y)
        INTEGER, INTENT(IN) :: nXi, nXj, nHi, nHj           ! Dimensions of Input, Kernel
        INTEGER, INTENT(IN) :: nYi, nYj                     ! Dimensions of Output - MUST BE nXi+nHi-1, nXj+nHj-1
        REAL, INTENT(IN) :: X(0:nXi-1, 0:nXj-1)   ! Input
        REAL, INTENT(IN) :: H(0:nHi-1, 0:nHj-1)   ! Kernel
        REAL, INTENT(OUT) :: Y(0:nYi-1, 0:nYj-1)  ! Output
        integer :: im, in, ii, ij, ip, iq
      
        Y = 0.0d0
        if( nYi .ne. nXi+nHi-1  .or. nYj .ne. nXj+nHj-1 ) RETURN
      
        do in = 0, nYj-1
            do im = 0, nYi-1
                do ij = 0, nXj-1
                    do ii = 0, nXi-1
                    ip = im - ii
                    iq = in - ij
                    if (ip .ge. 0 .and. ip .le. nHi-1 .and. iq .ge. 0 .and. iq .le. nHj-1) then
                        Y(im, in) = Y(im, in) + X(ii, ij) * H(ip, iq)
                    endif                      
                    enddo
                enddo
            enddo
        enddo
        return
    end subroutine conv2

Tuesday, December 25, 2007

Notes Matlab

Notes Matlab
============

Contents
=========
Help
MEX - C


MEX - C
========

A list of mx-MEX functions is found in:
Help -> MATLAB -> C and Fortran Functions

Mex interface in C code:
******************
void mexFunction(
int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* more C code ... */
******************



Help:
=====
>matlab -help

Setup Matlab's path - save the following into startup.m
*********************
% Add my test directory as the first entry
% on MATLAB's search path

path('/import/george/Applied/chee/programs/RKohn/SallyW',path)
*********************

To Run Matlab m-file, just type the name WITHOUT the ".m"
eg to run Hello.m, type "Hello"


Running Matlab:
1. GUI environment: /usr/local/matlab/bin/matlab
2. NO GUI: /usr/local/matlab/bin/matlabl -nojvm



*********************
RESHAPE

>> crshp(:,:,1)=[1,2,3,4;5,6,7,8;9,10,11,12]
>> crshp(:,:,2)=[13,14,15,16;17,18,19,20;21,22,23,24]
crshp(:,:,1) =
1 2 3 4
5 6 7 8
9 10 11 12
crshp(:,:,2) =
13 14 15 16
17 18 19 20
21 22 23 24

>> reshape(crshp(:,:,1), 12, 1)
ans =
1
5
9
2
6
10
3
7
11
4
8
12