Friday 12 July 2013

Speedup Matlab Calculations

Improving the Speed of MATLAB Calculations

Large scale numerical calculations can put heavy demands on your computer. Some software companies argue that they do not have to do a good job of performance tuning because computer hardware is advancing so rapidly. While it may be possible to foist slug-like word-processors onto the average computer user, engineers and scientists are usually less tolerant of poorly designed and implemented software.
In technical markets the gains in computing performance raise, not lower, the expectations of users. It is often the case that improved hardware and software allows simulations to be run with more detail, which often translates to more realistic results.
MATLAB programs are interpretted. This would seem to make it inapproapriate for large scale scientific computing. The power of MATLAB is realized with its extensive set of libraries which are compiled or are carefully coded in MATLAB to utilize ``vectorization''. The concept of vectorization is central to understanding how to write efficient MATLAB code.
Vectorized code takes advantage, wherever possible, of operations involving data stored as vectors. This even applies to matrices since a MATLAB matrix is stored (by columns) in contiguous locations in the computer's RAM. The speed of a numerical algorithm in MATLAB is very sensitive to whether or not vectorized operations are used.
This section presents some basic considerations to writing efficient MATLAB routines. It is possible, of course, to become obsessed with performance and waste man-hours of programming time in order to save a few seconds of execution time. The ideas presented below require little extra effort, yet can yield significant speed improvements. Once you understand how to use these procedures they will become natural parts of your MATLAB programming style.
For a more advanced discussion of vectorization techniques be sure to check the Mathworks Home Page and in particular their technical notes
The following topics are covered in this section
  • Using vector operations instead of loops
  • Pre-allocating memory for vectors and matrices
  • Correct code is most important



Using vector operations instead of loops


Consider the following loop, translated directly from Fortran or C
dx = pi/30;
nx = 1 + 2*pi/dx;
for i = 1:nx
x(i) = (i-1)*dx;
y(i) = sin(3*x(i));
end
The preceding statements are perfectly legal MATLAB statements, but they are an inefficient way to create the x and y vectors. Recall that MATLAB allocates memory for variables on the fly. On the first time through the loop (i=1), MATLAB creates two row vectors x and y, each of length one. On each subsequent pass through the loop MATLAB appends new elements to x and y. Not only does this incur extra overhead in the memory allocation calls, the elements of x and y will not be stored in contiguous locations in RAM. Thus, any subsequent operations with x and y, even though these operations may be vectorized, will take a performance hit because of memory access overhead.
The preferred way to create the same two x and y vectors is with the following statements.
x = 0:pi/30:2*pi
y = sin(3*x);
The first statement creates the row vector, x, with 1 + pi/15 elements stored in contiguous locations in RAM. The second statement creates a new (matrix) variable, y, with the same number of rows and columns as x. Since x is a row vector, as determined by the preceding step, y is also a row vector. If x were, for example, a 5 by 3 matrix, then y = sin(3*x) would create a 5 by 3 matrix, y.
MATLAB is designed to perform vector and matrix operations efficiently. To take maximum advantage of the computer hardware at your disposal, therefore, you should use vectorized operations as much as possible.

Pre-allocating memory for vectors and matrices

Though MATLAB will automatically adjust the size of a matrix (or vector) it is usually a good idea to pre-allocate the matrix. Pre-allocation incurs the cost of memory allocation just once, and it guarantees that matrix elements will be stored in contiguous locations in RAM (by columns).
Consider the following (admitedly artificial) sequence of statements.
dx = pi/30;
nx = 1 + 2*pi/dx;
nx2 = nx/2;
nx = 1 + 2*pi/dx;
nx2 = nx/2;
for i = 1:nx2
x(i) = (i-1)*dx;
y(i) = sin(3*x(i));
end
for i = nx2+1:nx
x(i) = (i-1)*dx;
y(i) = sin(5*x(i));
end
Since we know the size of x and y, a priori, we can pre-allocate memory for these vectors. Pre-allocation involves creating a matrix (or vector) with one vectorized statement before any of the matrix elements are referenced individually. The ones and zeros functions are typically used to pre-allocate memory.
Here is an improvement of the preceding statements with pre-allocation of memory for x and y.
dx = pi/30;
nx = 1 + 2*pi/dx;
nx2 = nx/2;
for i = 1:nx2
nx = 1 + 2*pi/dx;
nx2 = nx/2;
x = zeros(1,nx); % pre-allocate row-vectors, x
y = zeros(1,nx); % and y
for i = 1:nx2
x(i) = (i-1)*dx;
y(i) = sin(3*x(i));
end
for i = nx2+1:nx
x(i) = (i-1)*dx;
y(i) = sin(5*x(i));
end
The statements x(i) = ..., and y(i) = ... still do not take advantage of possibilities for vectorization, but at least the elements of x and y are stored contiguously in RAM. We will improve the efficiency of the loops shortly. First, however, note that we could have written the pre-allocation statements as
x = zeros(1,nx); % pre-allocate row-vectors, x
y = x; % and y
The statement y = x does not mean that y will stay equal to x. It simply creates another matrix y with the same ``shape'' as x. Understanding that pre-allocation is important for efficiency will help you understand these apparently confusing twists of MATLAB programming logic.
We can further improve our loops by pulling the assignment of x out of the loops.
x = 0:pi/30:2*pi; % vectorized calculation of x
nx = length(x);
nx2 = nx/2;
for i = 1:nx2
nx = length(x);
nx2 = nx/2;
y = x; % pre-allocate memory for y
for i = 1:nx2
y(i) = sin(3*x(i));
end
for i = nx2+1:nx
y(i) = sin(5*x(i));
end
Finally, if we're obsessed with performance, we observe that the calculation of y can also be vectorized. To do this we use the colon notation to refer to segments of the x and y vectors.
x = 0:pi/30:2*pi; % vectorized calculation of x
nx = length(x);
nx2 = nx/2;
nx2 = nx/2;
Y (1: nx2) = sin (3*x (1: nx2)); % compute first part of y
nx = length(x);
nx2 = nx/2;
y = x; % pre-allocate memory for y
y(1:nx2) = sin(3*x(1:nx2)); % compute first part of y
y(nx2+1:nx) = sin(5*x(nx2+1:nx)); % and the second part
To those new to MATLAB programming, the preceding statements may appear unecessarily obfuscated. The comment statements, of course, help, but the logic behind the logic comes from a true understanding of vectorization. Once you get the hang of MATLAB's colon notation you too will come to write code like this. Whenever the speed of MATLAB code is important, there is no substitute for vectorization.



Monday 8 July 2013

MATLAB is the best tool ever for SIMULATION


MATLAB is the tremendous tool today for most kind of simulation purposes with great functionality.

MAT stands for Matrix and LAB stands for Laboratory.

I am working on some of the toolboxes of MATLABs are"

1. Image Processing
2. Neural Network
3. Signal Processing
4. Filter Designing
5. Computer Vision
6. Communication Systems etc.




Thursday 1 November 2012

Protect your email and facebook account from being hacked


Following precautions will help you to protect yourself from phishing attacks..........


1. Do not click on links which redirect you to email's, facebook's or any social site's     login page.

2. Always open Email's  or Socials Site's Login Page by typing manually on address bar of your browser.
     e.g.   For Gmail Login type 'www.gmail.com' or for other login pages like               'www.mail.yahoo.com' or 'www.facebook.com' etc.

3. If you are a busy person and can't keep eye on address bar then on another option is    make your own HTML page of having links of 'GMAIL', 'YAHOO MAIL', 'FACEBOOK' and other    Login Pages you use frequently, and save on your desktop.

If you have any suggestions or more tips your comments are welcome.............
Enjoy, and Happy Surfing................... 







Sunday 27 May 2012

Vb.Net Function that converts currency from figures into words (Designed For Indian Currency )

Copy and paste into ur module to use directly on any windows form this can be used in web pages also.....


 ''' <summary>
    ''' This is the function which converts currency from figures into words
    ''' </summary>
    ''' <param name="Number"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Figures_To_Words(ByVal Number As String) As String
        Figures_To_Words = "Rs."
        Dim arrNUM As String() = Split(Number, ".", )

        If Len(arrNUM(0)) = 1 Then
            Figures_To_Words &= SingleDigit(arrNUM(0))
        ElseIf Len(arrNUM(0)) = 2 Then
            Figures_To_Words &= TwoDigit(Right(arrNUM(0), 2))
        ElseIf Len(arrNUM(0)) = 3 Then
            Figures_To_Words &= ThreeDigit(arrNUM(0))
        ElseIf Len(arrNUM(0)) = 4 Then
            Figures_To_Words &= FourDigit(arrNUM(0))
        ElseIf Len(arrNUM(0)) = 5 Then
            Figures_To_Words &= FiveDigit(arrNUM(0))
        ElseIf Len(arrNUM(0)) = 6 Then
            Figures_To_Words &= SixDigit(arrNUM(0))
        ElseIf Len(arrNUM(0)) = 7 Then
            Figures_To_Words &= SevenDigit(arrNUM(0))
        End If

        Return UCase(Figures_To_Words & " Only")
    End Function

    Function SingleDigit(ByVal Digit As String) As String
        Select Case Val(Digit)
            Case 1 : SingleDigit = " One"
            Case 2 : SingleDigit = " Two"
            Case 3 : SingleDigit = " Three"
            Case 4 : SingleDigit = " Four"
            Case 5 : SingleDigit = " Five"
            Case 6 : SingleDigit = " Six"
            Case 7 : SingleDigit = " Seven"
            Case 8 : SingleDigit = " Eight"
            Case 9 : SingleDigit = " Nine"
            Case Else : SingleDigit = ""
        End Select
    End Function
    
    Function TwoDigit(ByVal Digit As String) As String
        TwoDigit = ""
        If Len(Digit) = 2 AndAlso Left(Digit, 1) = "1" Then
            Select Case Digit
                Case "10" : TwoDigit &= " Ten "
                Case "11" : TwoDigit &= " Eleven"
                Case "12" : TwoDigit &= " Twelve"
                Case "13" : TwoDigit &= " Thirteen"
                Case "14" : TwoDigit &= " Fourteen"
                Case "15" : TwoDigit &= " Fifteen"
                Case "16" : TwoDigit &= " Sixteen"
                Case "17" : TwoDigit &= " Seventeen"
                Case "18" : TwoDigit &= " Eighteen"
                Case "19" : TwoDigit &= " Nineteen"
            End Select
        ElseIf Val(Digit) >= 20 Then
            Select Case Left(Digit, 1)
                Case "2" : TwoDigit &= " Twenty"
                Case "3" : TwoDigit &= " Thirty"
                Case "4" : TwoDigit &= " Forty"
                Case "5" : TwoDigit &= " Fifty"
                Case "6" : TwoDigit &= " Sixty"
                Case "7" : TwoDigit &= " Seventy"
                Case "8" : TwoDigit &= " Eighty"
                Case "9" : TwoDigit &= " Ninty"
            End Select
            TwoDigit &= SingleDigit(Right(Digit, 1))
        End If

    End Function
    
    Function ThreeDigit(ByVal Digit As String) As String
        ThreeDigit = SingleDigit(Left(Digit, 1)) & " Hundreds" & TwoDigit(Right(Digit, 2))
    End Function
    
    Function FourDigit(ByVal Digit As String) As String
        FourDigit = SingleDigit(Left(Digit, 1)) & " Thousands" & ThreeDigit(Right(Digit, 3))
    End Function
    
    Function FiveDigit(ByVal Digit As String) As String
        FiveDigit = TwoDigit(Left(Digit, 2)) & " Thousands" & ThreeDigit(Right(Digit, 3))
    End Function
    
    Function SixDigit(ByVal Digit As String) As String
        SixDigit = SingleDigit(Left(Digit, 1)) & " Lacs" & FiveDigit(Right(Digit, 5))
    End Function
    
    Function SevenDigit(ByVal Digit As String) As String
        SevenDigit = TwoDigit(Left(Digit, 2)) & " Lacs" & FiveDigit(Right(Digit, 5))
    End Function