Anonymous functions in MATLAB/OCTAVE – A Simple Example

In the previous post, I showed you lambda function in python and how to combine it with built-in filter function. As you may know, lambda is also known as anonymous function. In this post, we take a look at using anonymous functions in MATLAB/OCTAVE.

It is really easy, in MATLAB/OCTAVE you need to define the anonymous function using a handle sign (i.e. @): it would be something like this:

f = @(x)(x^3 + x^2 + x + 1)


It can be seen that after the @, the parameters are listed between parenthesis. Then you mention the function embraced with another couple of parenthesis.
After you defined the anonymous function, now it can be used in many ways. For instance you can evaluate it, like this:

ans = 25.375

You can also, calculate integrals, for instance:


ans = 2.0833


Let’s do all that in one line of code:

quad(@(x)(x^3 + x^2 + x + 1),0,1)
ans = 2.0833

Now consider a case that you have more than a variable in your function. It is simple, NO?

f=@(x,y)(x^2 + y^3  + x*y)
ans = 5.1250

And the last and the most interesting tip here. In MATLAB/OCTAVE we can have array of anonymous functions, for instance:

f={@(x) (x+2);
@(y) (3*y + y^0.5);
@(z) (sin(z)+cos(z));


ans = 2.200
ans = 1.4477
ans = -0.67526
ans = 0.087168

Now, go crazy!!!

Posted in Linux, MATLAB, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Using ‘filter’ in python – a lambda function tip

When using filter in python, one of the most helpful functions is lambda. Firstly, because you do not want to name and separately save any simple function. Secondly, lambda is a fast to code function, if you know how to. I give you some example, let’s first see how to use lambda function. The following piece of code shows a normal function, including a return term:

def f(x):
return x+ x**2 + x**3

Now if we want to use lambda function, the code would be as:

g = lambda x: x+ x**2 + x**3

As you can see, after lambda you should mention the parameters of the function and then “:” as usual in python. And then you mention the function without any return term.

filter is a  built-in function in python.
According to python documentation, filter constructs a list from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If iterable is a string or a tuple, the result also has that type; otherwise it is always a list. If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.

Now we want to combine filter with lambda to get more efficient and simpler code. Check the following example out:

# define an array
Ar = [3,67,23,55,90]
# now use lambda to find elements divisible by 3
print filter(lambda x: x % 3 ==0, Ar)

The result would be as follows:



Posted in Linux, Optimization, programming, Python, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Modify the bibliography style in Latex – Part.1

When using IEEEtran bibliography style, authors’ name are something like following:

In case you need to have full first name for each author, you need to perform the following steps:


1- open IEEEtran.bst and find the following line:

FUNCTION {}{ "{f.~}{vv~}{ll}{, jj}" }

This line shows the format has been used by default for presenting the first, middle, and last names.

2- Replace the line with the following line:

FUNCTION {}{ "{ff~}{vv~}{ll}{, jj}" }

3- save the file and compile the latex project again, you should get results similar to following:




Note that, you need to have the full names in your bib file (bibliography database), otherwise the software cannot guess by itself. ;)

Posted in Linux, Thoughts, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Make PowerPoint presentation using MATLAB – toPPT

Short description:

toPPT is a powerful tool for generating PowerPoint presentations programmatically defined in MATLAB. It will use different scripts to perform exports of figures, tables and texts. For this purpose it will use scripts written by Jens Richter

main article + example

toPPT accepts the following predefined TeX parameters:

   'alpha','upsilon','sim','beta','phi','leq','gamma','chi', ...
   'infty','delta','psi','clubsuit','epsilon','omega','diamondsuit', ...
   'zeta','Gamma','heartsuit','eta','Delta','spadesuit','theta', ...
   'Theta','leftrightarrow','vartheta','Lambda','leftarrow','iota', ...
   'Xi','uparrow','kappa','Pi','rightarrow','lambda','Sigma', ...
   'downarrow','mu','Upsilon','circ','nu','Phi','pm','xi','Psi','geq', ...
   'pi','Omega','propto','rho','forall','partial','sigma','exists', ...
   'bullet','varsigma','ni','div','tau','cong','neq','equiv','approx', ...
   'aleph','Im','Re','wp','otimes','oplus','oslash','cap','cup', ...
   'supseteq','supset','subseteq','subset','int','in','o','rfloor', ...
   'lceil','nabla','lfloor','cdot','ldots','perp','neg','prime', ...
   'wedge','times','0','rceil','surd','mid','vee','varpi','copyright', ...
Posted in Linux, MATLAB, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , | Leave a comment

Dealing with SVG files – issue no.1 (groupping issue)

Sometimes you have an SVG file and you want to add some objects to that. So you open the fine using Inkscape and make the object and insert it somewhere in the workspace. Afterwards, you want to save the result as a eps, png, …. but you do not see the new objects you have just added to the file. Let’s clarify it using an example. OK. consider the following simple file is open in Inkscape:


It just contains a red rectangular and three vertical texts. Now I add a new rectangle:


OK, you see the tiny blue rectangle. I have to mention again that what we are talking about is a bug so it might happen occasionally not all the times. Now I want to save the result as an EPS file and let’s assume that the bug occurs, so the EPS file would be like the following:


You can see that the blue rectangle is not in the EPS file.

In order to solve this problem, you need to go back to the Inkscape and perform the following steps:

  1. select all (Ctrl+A)
  2. keep un-grouping (Ctrl+Shift+G)
  3. and then save the final result as an EPS file
Posted in Linux, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Algorithm = AL-KHWARIZMI

Muhammad Ibn Musa Al-Khwarizmi (c 780 AD to c 850 AD) was a Muslim  mathematician whose works introduced our modern numerals (the Hindu-arabic numerals) to Europe, and the title of his book Kitab al-jabr wa al-muqabalahprovided the source for the term algebra. His name is the source for the term algorithm.

Posted in Uncategorized | Leave a comment

Convert Image in MATLAB (bmp, gif, hdf, jpg, jpeg, jp2, jpx, pbm, pgm, ppm, pnm, pcx, png, ras, tif, tiff, xwd)

In MATLAB you can easily convert your image from a specific format to another format in the following list:
bmp, gif, hdf, jpg, jpeg, jp2, jpx, pbm, pgm, ppm, pnm, pcx, png, ras, tif, tiff, xwd.
Although here I mention some examples, you can convert anything mentioned in the above list easily in 2 lines of code.

first of all you need to read the image file:

I = imread('myImage.png');

here I assume that I have an image with png format, but you can have other formats as well.

The next step is, convert it to your desired format and write it:


For more details check the MATLAB document page.

Posted in Linux, MATLAB, OpenCV, programming, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment