Thursday, December 3, 2009

Wow.. it's been such a long time since the last post!! I've been quite busy with ViBOT, specially during the last two weeks. Anyway, I thought it would be nice to write a post about some nice Matlab functions I found quite useful:

reshape:this is a nice function that lets you reshape any array or matrix into any other array or matrix with different dimensions, as long as the number of elements is kept the same. It is very useful when one wants to loop through every element of a two or three dimensional array. If A=[1 2; 3 4] then reshape(A,[1 4]) will return [1 2 3 4]. To get back to the original array we can then use reshape([1 2 3 4],[2 2]).

ind2sub:this is a useful function when manipulating arrays that were linearised with reshape. From Matlab help: "The ind2sub command determines the equivalent subscript values corresponding to a single index into an array". A simple example is the following:

A=[1 2; 6 5]; B = reshape(A,[1 4]);

[sV, sI] = sort(B, 'descend'); % sort linearised array

disp(sV(1)); %show max value: 6

disp(sI(1)); %show max index: 2

[x,y] = ind2sub( size(A), sI(1) );

disp(x); disp(y); % (x,y) == (2,1), we got the coordinates in the original matrix

There is also a sub2ind function which does the reverse transformation. ind2sub was very useful to ease the sorting task when applying the Hough transform for circle detection, where there is an accumulator matrix which is 3-dimensional.