Working with 3D matrix in Matlab

November 16, 2009 mohsenam Leave a comment

To visualize the 3D matrix just consider that them as stack of images or layers.

{There is one more very important way,  that is third dimension representing the feature vector’s length}

So a(:, :, i) = all elements in the layer i, so changing i will give you next image.

where a(r,c, i) will move in the image.

a = [];
a(:,1,:) = [111 112 113 114 ; 121 122 123 124 ; 131 132 133 134];
a(:,2,:) = [211 212 213 214 ; 221 222 223 224 ; 231 232 233 234];
a(:,3,:) = [311 312 113 114 ; 321 322 323 324 ; 331 332 333 334];
a(:,4,:) = [411 412 113 114 ; 421 422 423 424 ; 431 432 433 434];
a(:,5,:)= [511 512 513 514 ; 521 522 523 524 ; 531 532 533 534];

Will make the 3D matrix ‘a’

size(a)

ans =

     3     5     4

That is there are 4 images and each image is of 3 rows and 5 cols.

But let’s Say you want to represent 3rd dimension as the feature vector  so each a(r,c, : ) represents a feature vector.

Now let’s say you want to make a 3D matrix from one feature vector.


vt = squeeze([a( 2,1, : ) ] );
vt = vt(:)';
%//make it into 2 by 3 by length(feature) matrix
%//repmat will repeat this matrix and make a 6 row matrix
%//the reshape picks the elements from the 1st col, 1st row and start moving downward in the row and so on
%//therefore each time it will meet same element as it moves down the row and fills our first image
tempT = (reshape(repmat(vt, 2*3,1), 2, 3, length(vt)));
size(tempT)
ans =
     2     3     4

Matlab Memory management

November 12, 2009 mohsenam Leave a comment

Oh it turns out that the matlab does not copy the data while passing it to the function if the function is not changing the values.
Which is big saving.
http://blogs.mathworks.com/loren/2006/05/10/memory-management-for-functions-and-variables/#1

Running Variance

November 9, 2009 mohsenam Leave a comment

Just in mid of the code wanted to find the Running Variance, just as habit typed and found not so good links.

1/N*[sum(i=1:N-1, Xi*Xi') + X*X'] – (1/N^2)*[ sum(i=1:(N-1), Xi) + X][ sum(i=1:(N-1), Xi) + X]‘

Matlab code for calculating the running Variance is as follow

%updating Mean

newMean = (newMean*(N-1) + X)/N;

sumOfSq = sumOfSq*(N-1)  + X*X';

covMat =  (1/N)*sumOfSQ - newMean*newMean'

Externally there are few links that are worth reading

Using Many Colors in the Matlab

October 29, 2009 mohsenam Leave a comment

While using the ‘plot’ function you end up using only small number of colors. I searched online to see if there are any solutions

One is given by http://blogs.mathworks.com/pick/2008/08/15/colors-for-your-multi-line-plots/

It discusses “varycolor” file. Which is very nice for generating many colors on the run.

However I found that for plotting of the poitns and data following is much distinguishable.

It is 72 element array.

clr = [ '.r';'.b'; '.g'; '.c';'.m'; '.y';...
        '*r';'*b'; '*g'; '*c';'*m'; '*y';...
        'xr';'xb'; 'xg'; 'xc';'xm'; 'xy';...
        '+r';'+b'; '+g'; '+c';'+m'; '+y';...
        'or';'ob'; 'og'; 'oc';'om'; 'oy';...
        'dr';'db'; 'dg'; 'dc';'dm'; 'dy';...
        'sr';'sb'; 'sg'; 'sc';'sm'; 'sy';...
        'pr';'pb'; 'pg'; 'pc';'pm'; 'py';...
        'hr';'hb'; 'hg'; 'hc';'hm'; 'hy';...
        'vr';'vb'; 'vg'; 'vc';'vm'; 'vy';...
        '>r';'>b'; '>g'; '>c';'>m'; '>y';...
        '.k';'xk'; '*k'; '+k';'ok'; 'dk';...
        ];

Matlab Efficient coding

October 14, 2009 mohsenam Leave a comment

The interpreter nature of Matlab keeps me always looking for coding it fast and efficient way, squeezing as much work as we can squeeze from single statement.

Searching for something about Matlab found following blog entry

http://omar-sabih.blogspot.com/2009/05/matlab-tryin-to-code-smarter.html

It’s much cleaner and better presented version is present at http://www.sajidmc.net/bn/2009/05/efficient-matlab-coding/

Although I also have my  Matlab page http://whatevericode.wordpress.com/matlab/ but it much less rigorous

SVMToolboxes

August 19, 2009 mohsenam Leave a comment

I have till now used 2 SVM toolboxes details about them are as follow.

1) SVM Tool Box for matlab

http://theoval.sys.uea.ac.uk/svm/toolbox/

Here we can use the precalculated kernels

NOTE: while compiling it is using the

mex smosvctrain.cpp InfCache.cpp LrrCache.cpp SmoTutor.cpp -lm

The -lm option does not exist atleast in the Matlab 7.0 mex function. So remove it.

While compiling LrrCache.cpp, the compiler is giving a stupid error about the ambiguous call to floor function at line 151. Just type cast it to (long double).

2) Gun’s SVM toolbox

http://www.isis.ecs.soton.ac.uk/resources/svminfo

NOTE:  Labels should be -1,1

The data is in row order i.e. each row is a feature vector.

Labels Have to be in column vector.

Categories: Matlab_Tips, SVM, matlab

stl Sort in C++; simple code for sorting the array of structures

February 18, 2009 mohsenam Leave a comment

Simple Code that shows how to use the C++ STL function sort()


<blockquote>
#include <iostream>
#include <algorithm>

using namespace std;

struct Numb{
double num;
int pos1;

bool operator &lt;(const Numb &amp;n1){
return num &lt; n1.num;
};
};
////////////////////////////////
void main(void){

Numb numb[10];

for(int i=0; i &lt; 10; i++){
numb[i].num = rand();
numb[i].pos1 = i;
}
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;
for(int i=0; i &lt; 10; i++){
cout &lt;&lt; " " &lt;&lt; "(" &lt;&lt; numb[i].pos1 &lt;&lt; ") " &lt;&lt; numb[i].num;
}
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;

std::sort( numb, numb+10);
cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;
for(int i=0; i &lt; 10; i++){
cout &lt;&lt; " " &lt;&lt; "(" &lt;&lt; numb[i].pos1 &lt;&lt; ") " &lt;&lt; numb[i].num;
}

cout &lt;&lt; endl &lt;&lt; "---------" &lt;&lt;endl;

}
Categories: STL, code Tags: , , , ,

OpenCv; Small Code using Erode and Dilate

February 16, 2009 mohsenam Leave a comment

Small Code that reads pbm image, convert it into 1 channel image. Erodes it, dilates it and shows them

#include <float.h>

#include <limits.h>

#include <time.h>

#include <ctype.h>

#include <iostream>

#include "BlobResult.h"

#include "blob.h"

#ifdef _EiC

#define WIN32

#endif

using namespace std;

void main(void){

IplImage *inputImage = cvLoadImage( "0584.pbm", 1 );

IplImage *gray = cvCreateImage( cvSize(inputImage->width,inputImage->height), 8, 1 );

cvCvtColor( inputImage, gray, CV_BGR2GRAY );

IplImage*img = inputImage;

inputImage = gray;

::cvReleaseImage(&img);

cvNamedWindow( "orig", 1 );

cvShowImage( "orig", inputImage );

IplImage *outputImage;

IplImage *outputImage2;

outputImage = cvCreateImage( cvSize( inputImage->width, inputImage->height ), IPL_DEPTH_8U, 1 );

outputImage2 = cvCreateImage( cvSize( inputImage->width, inputImage->height ), IPL_DEPTH_8U, 1 );

cvErode(inputImage, outputImage, 0, 1);

cvNamedWindow( "erode", 1 );

cvShowImage( "erode", outputImage );

 

cvDilate(outputImage, outputImage2, 0, 2);

cvNamedWindow( "dilate", 1 );

cvShowImage( "dilate", outputImage2 );

cvReleaseImage(&outputImage);

cvReleaseImage(&outputImage2);

cvWaitKey();

 

return;

}>

converting simple STL list to circular list

February 11, 2009 mohsenam Leave a comment

/////////////////////////////////////////////////////////
//converting simple STL list to circular list
/////////////////////////////////////////////////////////
list<int>::iterator nextIter(list<int>::iterator i, list<int>::iterator beg,  list<int>::iterator end){
i++;
if(i == end){i = beg;}
return i;
}
/////////////////////////////////////////////////////////
list<int>::iterator prevIter(list<int>::iterator i, list<int>::iterator beg,  list<int>::iterator end){
if(i == beg){i = end; i–;}
else{i–;}
return i;
}
/////////////////////////////////////////////////////////
void printL(list<int> &L){
cout << endl;
list<int>::iterator i;
for(i=L.begin(); i != L.end(); ++i) cout << *i << ” “;
cout << endl;
}

Categories: C++, STL Tags:

Simple STL code in C++

February 11, 2009 mohsenam Leave a comment

Just a general simple program for the Standard Template Library (STL) List

#include <list>
#include <iostream>
using namespace std;
////////////////////////////////
class codeWord{
public:
double vl[3];
double minI;

codeWord(){
minI = 0;
vl[0]=vl[1]=vl[2] = 0;
};
};
////////////////////////////////
void printList(std::list<codeWord *> &ml){
cout <<endl;

codeWord *ptr;
for(std::list<codeWord *>::iterator list_iter = ml.begin(); list_iter != ml.end(); list_iter++)    {
ptr = *list_iter;
std::cout<<” ” << ptr->minI<<endl;
}

cout <<endl;

};
////////////////////////////////
void main(void){

std::list<codeWord *> mlist;
codeWord a;
codeWord b;

a.minI = 10;
b.minI = 5;

mlist.push_front(&a);
mlist.push_front(&b);

printList(mlist);

};

Categories: STL Tags: ,