Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Saturday, January 25, 2014

Cholesky decomposition of positive definite matrices

 pascal(N) is the Pascal matrix of order N: a symmetric positive definite matrix with integer entries, made up from Pascal's triangle.  Its inverse has integer entries.

Generate a pascal matrix:

>> A = pascal(5)

A =

     1     1     1     1     1
     1     2     3     4     5
     1     3     6    10    15
     1     4    10    20    35
     1     5    15    35    70

(Ref: wikipedia)
In linear algebra, the Cholesky decomposition or Cholesky factorization is a decomposition of a Hermitian, positive-definite matrix into the product of a lower triangular matrix and its conjugate transpose, useful for efficient numerical solutions and Monte Carlo simulations. It was discovered by André-Louis Cholesky for real matrices. When it is applicable, the Cholesky decomposition is roughly twice as efficient as the LU decomposition for solving systems of linear equations.



>> R = chol(A)

R =

     1     1     1     1     1
     0     1     2     3     4
     0     0     1     3     6
     0     0     0     1     4
     0     0     0     0     1

Recover the matrix from its Cholesky decomposition and its transpose.

>> A1 = R' * R

A1 =

     1     1     1     1     1
     1     2     3     4     5
     1     3     6    10    15
     1     4    10    20    35
     1     5    15    35    70

Saturday, December 28, 2013

Solving a set of linear equations and finding the echelon form using MATLAB (LU or LR Factorization)

% factorization  of a set of n equations n unknowns...
% u + v + w = 6
% u + 2v + 2w = 11
% 2u + 3v - 4w = 3

A = [1 1 1 6; 1 2 2 11; 2 3 -4 3]
[L U] = lu(A);

% U contains the echelon form, or the upper triangular matrix.

U =

    2.0000    3.0000   -4.0000    3.0000
         0        0.5000    4.0000    9.5000
         0         0            7.0000   14.0000

Sunday, January 20, 2013

If font size in the figures do not change in Matlab..

If you wish to change the font of the text in your Matlab figure globally, then you have to set :

>>set(gca, 'FontSize', xx);

And if you want to change it only for a specific legend or text or axis-handles just add the option 'FontSize',  xx  to your parameters.

But this did not work in my Ubuntu 12.04 system. No matter what changes I made, Matlab displayed a fixed font size.

Thanks to the following forums, I could sort it out:
 
 
http://www.mathworks.in/matlabcentral/newsreader/view_thread/301184
http://ubuntuforums.org/showthread.php?t=1762805

You need to go to terminal and install following two packages, log in again.
 
$sudo apt-get install xfonts-75dpi
$sudo apt-get install xfonts-100dpi

Saturday, November 24, 2012

beautiful Matlab tutorials for advance usage

Found  an excellelent tutorial on Matlab for advance usage such as speed-up, debugging, using OOP.  Its been really useful to me, hope anyone who finds it will be equally delighted having found it :)

YAGTOM: Yet Another Guide TO Matlab

Enjoy!

Monday, November 19, 2012

UGM Library for Undirected Graphical Models

I was in search for  suitable library for CRF implementation for my segmentation work and found the UGM library useful, since it has some demos and nice explanations for different functionalities. It also seems to be up-to-date with different inference techniques.

Here is the link for the directory:
http://www.di.ens.fr/~mschmidt/Software/UGM.html

They have clearly explained the download and setup process. However, I found that bit scattered across different pages, so here are the steps that I went through:

1. Download the tgz from the homepage .
The 2011 version is available in www.di.ens.fr/~mschmidt/Software/UGM_2011.zip

2. There are also some updates in 2012. Download them as well.

3.Download the mex-wrapper for maxflow from the link http://www.mathworks.com/matlabcentral/fileexchange/21310-maxflow and place it under the directory UGM/

4.Download maxflow-v3.01.zip from  http://vision.csd.uwo.ca/code/ and place it inside UGM/maxflow/  under the name maxflow-v.3.0 (since the different files refer to it in the same name.)  Note: there is a  Readme.txt in UGM/maxflow/ to guide us too.

5.In matlab command window,
 >> cd UGM/
 >> addpath(genpath(pwd))
>> mex -setup
>> mexAll

In case you find errors like the following while running mexAll, sort it out using help from the other blogpost :  matlab-error-of-type-usrbinld

: /usr/bin/ld: matlab/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.10' not found (required by /usr/bin/ld) 

 

 5. >> cd maxflow/
      >> make
     >> test1 
     >> test2   % to test the functionality
      >> cd ..

In case you are in a 64-bit system, you will face an error like this:

>> make     
>> test1

??? Error using ==> maxflowmex
Function "mxGetIr_700" is obsolete in file "compat32.cpp", line 282.
(64-bit mex files using sparse matrices must be rebuilt with the "-largeArrayDims" option.  See the R2006b release notes for
more details.)


Error in ==> maxflow at 35
[flow,labels] = maxflowmex(A,T);

Error in ==> test1 at 32
[flow,labels] = maxflow(A,T)


The solution is, 

edit the file maxflow/make.m as follows:


mex -O -largeArrayDims maxflowmex.cpp maxflow-v3.0/graph.cpp maxflow-v3.0/maxflow.cpp


Then
>> make
>> test1 
>> test2   % to test the functionality
>> cd ..


6.  Run any demo present in UGM/examples/. For example,
  >> example_UGM_AlphaBeta 

Friday, November 9, 2012

Handling Sparse matrices in Matlab

Matlab has its own memory limits due to which we have to define very large matrices
as sparse and use them. But we can not use them exactly the same way as normal matrice
due to difference in the way of storage. In my case, I have to define neighborhodd of 
pixels in an image which is usually of large size(307200 for a 640x480 image!!!). So,
the nbrhood matrix (lets call it adj) is now of size 307200x307200, which is huge.
 
I posted this in stackoverflow and had a beautiful discussion over the forum.
  
>>nNodes = 50400;
 
>>adj = sparse(nNodes,nNodes); %adj is a large sparse matrix with all values as zero.
 
I want to insert a 1 to adj(i,j) if pixel i and j are adjacent.
 
>>adj(sub2ind([nNodes nNodes], ind, ind + 1)) = 1; %ind is a vector of indices
??? Maximum variable size allowed by the program is exceeded.
 
Therefore, it is better to define adj in the following way, where definition
and updation take place in one shot. 
 
>>adj = sparse(ind, ind + 1, ones(size(ind)), nNodes, nNodes, length(ind));
 
Now, how do we access sparse matrix elements?
 
In a general matrix, we can access and modify the elements, 
either by using 2-dimensional indexing  or linear indexing.
 
for example,
 
>> a = [ 1 2 3; 4 5 6]
a =
     1     2     3
     4     5     6

>> a(1,2)
ans =
     2

>> a(3)
ans =
     2

 
Similarly,
>> i = find(a == 2)
i =
     3
>> [i,j] = find(a == 2)
i =
     1
j =
     2

But we do not have the luxury to access using linear index 
for sparse matrices.
 
If I want to access non-zero elements of sparse matrix, I have to 
access the following way:
 
>> [i,j] = find(adj); 

The elements with higher indices must be accessed with 
2-dimensional indexing:

>> adj(nNodes, nNodes)
ans =
     0

>> adj(nNodes * nNodes)
??? Maximum variable size allowed by the program is exceeded.
 
That's it with sparse matrices for the time being. :)
 

Sunday, November 4, 2012

Matlab error of the type : /usr/bin/ld: matlab/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.10' not found (required by /usr/bin/ld)

When I tried to compile mex files in Matlab (R2009a, 32-bit) on my Ubuntu 11.10 system, I got the following error:

>>mexAll

/usr/bin/ld: /home/swagatika/softwares/matlab/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.10' not found (required by /usr/bin/ld)
/usr/bin/ld: /home/swagatika/softwares/matlab/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by /usr/bin/ld)
/usr/bin/ld: /home/swagatika/softwares/matlab/sys/os/glnx86/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by /usr/bin/ld)
collect2: ld returned 1 exit status

    mex: link of ' "minFunc_2009/lbfgsC.mexglx"' failed.


Well, I found that I upgraded my system from v10.10 to 11.10. In the process, the libraries stored in Matlab were not updated with the system libraries.

So when such an error occurs,

1) Locate the library file in the system.
2)Remove or create a back up of the library in Matlab path.
3)Create a soft link in the matlab's appropriate folder for the one present in /usr/lib/

$ locate libstdc++.so.6
/home/swagatika/.dropbox-dist/libstdc++.so.6
/home/swagatika/softwares/matlab/sys/os/glnx86/libstdc++.so.6
/home/swagatika/softwares/matlab/sys/os/glnx86/libstdc++.so.6.0.9
/home/swagatika/softwares/matlab/toolbox/shared/hdllink/scripts/linux32/libstdc++.so.6
/home/swagatika/softwares/matlab/toolbox/shared/hdllink/scripts/linux32/libstdc++.so.6.0.9
/home/swagatika/softwares/matlab/toolbox/symbolic/mupad/linux/lib/gcclibs/libstdc++.so.6
/usr/lib/i386-linux-gnu/libstdc++.so.6
/usr/lib/i386-linux-gnu/libstdc++.so.6.0.16
/usr/lib/ure/lib/libstdc++.so.6


$ln -s /usr/lib/i386-linux-gnu/libstdc++.so.6 libstdc++.so.6

Now it compiles properly.

Monday, October 29, 2012

confusion in VL_SLIC

The input image for segmentation:

Way1: Read the img as im2double , so the intensity ranges from 0 to 1. Here, the output superpixels are more regular and spacious.

%Input
img = im2double(imread('Images/2_14_s.bmp'));
regionSize = 30 ;
regularizer = 0.1;

%get the SLIC superpixels
vl_setup
segments1 = vl_slic(single(img), regionSize, regularizer) ;












Way2: Read the img as uint8 , so the intensity ranges from 0 to 255.
Here, the output superpixels are more irregular but tightly fit to the edges.

%Input
img = imread('Images/2_14_s.bmp');
regionSize = 30 ;
regularizer = 0.1;

%get the SLIC superpixels
vl_setup
segments1 = vl_slic(single(img), regionSize, regularizer) ;
I want to know which is the right way? and why this change is affecting the performance?