MATLAB: Search a vector in a matrix

I often encounter situations that require doing a vector search in a matrix. Finding discrete matrix elements is quite simple using the find command, but what about finding a vector?

Am aware of 3 of the possibly many options, namely: ismember, strmatch and strfind. But which of these to use for optimal speed? Turns out, strfind is the fastest of the three. Try the following code and be amazed at the order of magnitude difference in its search capability!

%—–define matrix a and vector b———%
a = [1 2 3;4 5 6;7 8 9;10 11 12;13 14 15]’; b = [7 8 9];
%—–using strfind
tic;
pos = strfind((a(:))’,b);
pos = round(pos/size(a,1))+1
disp(toc);
%—–using strmatch
tic;
pos = strmatch(b,(a)’)
disp(toc);
%—-using ismember
tic;
pos = ismember(a,b);
find(sum(pos) == 3)
disp(toc);

Simulation of the above (even such a small matrix) results in average time as 1.91e-4 sec, 6.3e-4 sec and 0.0019 sec respectively! Moral of the story: avoid ismember!

MATLAB: Using ANN library wrapper in Windows XP

ANN (Approximate Nearest Neighbours) is an extremely useful library when working with any type of application where the properties of data points are influenced by their neighbours. The MATLAB MEX-wrapper for the same provided by Shai Bagon, along with the patches, compiles effortlessly in Linux (have tried on F16 and F17). However, making the same work in Windows turned out to be quite an effort. First, the MATLAB inbuilt compiler for Windows (lcc) continuously kept crashing whenever I tried executing the library, and second, the lack of pre-existing solid C/C++ compiler made things difficult.

After searching on the net for free and supported compilers for MATLAB R2010a, I first tried Open Watcom, but it gave too many errors during compilation of the mex-wrappers.¬† Then it was the turn to check out MS Visual C++ Express Edition. Tried on a friend’s system having MS-VC++ 2010, and it too gave quite a few errors during compilation. Thought to try out MS-VC++ 2008, and guess what, it worked out of the box and the mex-wrapper compiled with quite an ease ūüôā . Wonder why MS-VC++ 2010 created issues..

Install Nvidia Driver and CUDA Toolkit on CentOS 6

( Update: have posted a MUCH simpler method of driver install. Steps for CUDA toolkit install have to be followed as given in this post, i.e. , bulleted step # 10 – 19 )

Although the topic has been addressed succinctly in a CentOS forum post, there are certain things like plymouth configuration post Nvidia driver install, etc. which I felt needed to be jotted down for reference. So, here we go describing the Nvidia CUDA toolkit installation on a CentOS system:

  • Download the appropriate toolkit, driver and SDK from Nvidia’s website.
  • RHEL, and its derivatives come with the open source Nvidia driver called nouveau. Before installing Nvidia drivers, we need to ensure nouveau drivers dont get loaded. For this, append the following in the line starting with ‘kernel’ in the file /etc/boot/grub.conf:

rdblacklist=nouveau nouveau.modeset=0

  • Install the Development Tools and Development Libraries group packages, and a few extra packages listed below:

sudo yum groupinstall ‘Development Tools’ ‘Development Libraries’

sudo yum install kernel-devel gcc-c++ freeglut freeglut-devel libX11-devel mesa-libGLU-devel libXmu-devel libXi-devel gcc* compat-gcc* compat-glibc* compat-lib*

  • Restart the system. Upon restart, you’ll see that the resolution of the display would have gone for a toss. Thats due to blacklisting the nouveau driver, and is a sign that we are on track! Open terminal and type the following to goto non-GUI mode (called, runlevel 3):

sudo init 3

  • Above command takes us to text mode. Change directory to /usr/src/kernels/ and note down the complete path of the kernel folder present. In our scenario, it shows up as:

/usr/src/kernels/2.6.32-220.13.1.el6.x86_64/

  • Change directory to the folder containing the downloaded files from Nvidia’s website (say ~/Downloads). Mark the 3 downloaded files as executables:

cd ~/Downloads

chmod a+x NV*; chmod a+x cuda*; chmod a+x gpu*

  • Now finally, we are ready to run the installer. First is the Nvidia Driver install :

sudo sh NVIDIA-Linux-x86_64-295.20.run –kernel-source-path=/usr/src/kernels/2.6.32-220.13.1.el6.x86_64/

  • NOTE : there’s a double minus sign before the word kernel above. During the above install, accept the licence agreement shown. Reboot upon completion:

sudo reboot

  • You’ll notice that the GUI resolution is back to normal, indicating successful Nvidia driver install. Now, cudatoolkit has to be installed.
  • Open terminal and change directory to ~/Downloads. Run the cudatoolkit*.run file:

sudo sh cudatoolkit_4.0.17_linux_64_rhel6.0.run

  • During the install, you’ll be asked to supply installation path. Enter the default path itself (/usr/local/cuda).
  • Once completed, few more steps are needed, like adding /usr/local/cuda to default path environment variable, etc. :

sudo nano /etc/ld.so.conf.d/cuda.conf

  • Add the following lines to the above created file :

/usr/local/cuda/lib64
/usr/local/cuda/lib

  • Save the above file by pressing Ctrl+x, followed by ‘y’ and pressing Enter. Now run:

sudo ldconfig

  • For adding cuda install path to enviroment path variable, edit ~/.bash_profile file using a text editor (say, nano ~/.bash_profile) :

export CUDA_INSTALL_PATH=/usr/local/cuda
export PATH=($PATH: /usr/local/cuda/bin)
export LD_LIBRARY_PATH=/usr/local/cuda/lib64
export PATH=($PATH: /usr/local/cuda/lib)

  • Finally, gpucomputingsdk needs to be installed. For that :

sh gpucomputingsdk_4.0.17_linux.run

  • During the install , you’ll be asked for install path. Keep in mind that the sdk can take around 400-500MB. Say, we install it to ~/Documents/NVIDIA_GPU_Computing_SDK.
  • Once done, we need to compile the files in the SDK:

cd ~/Documents/NVIDIA_GPU_Computing_SDK/C/

make

  • To check whether everything is working fine, we’ll run the deviceQuery file, provided by the SDK just installed:

cd ~/Documents/NVIDIA_GPU_Computing_SDK/C/bin/linux/release/

./deviceQuery

  • You should see something similar to what is shown at this link .

Congrats for successfully installing the Nvidia driver and the cudatoolkit on your CentOS system. I know, things should be much simpler. I wish someday Nvidia open-sources their drivers and CUDA toolkit to make things simpler for Linux enthusiasts. All that is left now, is to fix the ugly white-blue scrolling bar that shows up instead of the beautiful Plymouth at boot.

  • Reboot system. At the grub prompt, press ‘e’ against the first item on the grub menu to edit its kernel arguments (this mode is called kernel edit mode).
  • Out the the three lines being shown (starting with : root; kernel; initrd ), scroll to kernel line and press ‘e’ again. Type in the following at the end of that line:

vga=ask

  • Press Enter after typing the above and press ‘b’. You’ll see a tabulated list of keywords against several screen-resolutions. Note down the number being shown against the¬† most appropriate screen resolution. Say, the number is 361. Now, reboot system. Again, enter the kernel edit mode described in the previous two list items. The only difference being that instead of vga=ask, enter vga=0x361. Now press ‘b’ and you’ll see the beautiful Plymouth back in its glory!
  • To make this change permanent, open terminal and open /boot/grub/grub.conf as sudo (sudo nano /boot/grub/grub.conf).
  • Find the line saying ‘kernel’ against your most recent kernel version and add vga=0x361. Save the file (Ctrl+x, y, Enter)

Plymouth theme will now show up everytime that you’ll reboot. Enjoy!

MATLAB: Running codes in mixed OS environment

Ever faced the problem of running executables in a MATLAB installation on Linux. I frequently use an executable file (.exe) provided by an eminent research lab, but the linux enthusiast that I am, shifting to Windows just for running this file is out of the question! So, how to solve this issue!

Well, you need access to either a system running Windows at your workplace, or have a virtual Windows installation in VirtualBox. Set up ssh via cygwin (refer SSH via CYGWIN) on such a system and also make sure to have a password-less SSH login enabled (refer ssh autologin) between the Linux and Windows systems.

Once this is done and given the fact that such a Windows environment is up and running, all you have to do through the MATLAB command prompt is:

system(‘ssh username@IP_of_windows command1;command2’);

where : command1 and command2 are the things you want to execute on that system. For e.g: in my scenario, i have already put the .exe (say: test.exe) in the cygwin home folder (C:/cygwin/home/<username>/) on the Windows system. Now, i just run:

system(‘ssh username@IP_of_windows ./test.exe’);

You can use few more ssh/scp via system command calls in MATLAB to copy back & forth the data. Thats it!

MATLAB: Run m-file From Linux Terminal / DOS

Yes, MATLAB m-files can very much be executed without initiating the MATLAB GUI. This can be quite handy when when running multiple codes simultaneously without creating a huge clutter for the user, or when running multiple instances of MATLAB and there is a need to prevent unnecessary eating up of the RAM through its GUI.

In Linux, it works via the re-direction operator ( < ), while in Windows, it has to be done using the -r flag. The steps are detailed below:

For MATLAB installations on Linux, ensure the MATLAB soft link is available in /usr/local/bin, so that it can be called from the terminal without specifying its full installation path, i.e.,

cd /usr/local/bin

sudo ln -s /path_to_MATLAB_installation/matlab

cd

matlab -desktop

Once MATLAB link has been created, exit the MATLAB GUI. Now run your m-file (say: test_run.m) from the terminal as:

matlab -nodesktop -nosplash < /path_to_file_location/test_run -logfile test_run.log

Thats it! The “-nodesktop” option ensures the full GUI isnt initiated and only the MATLAB command prompt pops up, “-nosplash” prevents the MATLAB splash screen from showing up. The redirection operator will run all the commands in the m-file as they would in a normal way and “-logfile” logs all that shows up in the MATLAB command window.

In Windows, the slight modification is that we have to first cd to the location where the m-file is present

cd path_to_file_location\

matlab.exe -nodesktop -nosplash -r test_run -logfile test_run.log

And we are done!

P.S : 1) Dont forget to check out the difference in RAM usage with “-nodesktop” enabled and normal GUI way

2) DO NOT put .m with the file name when using this command, else it’ll result in an error!

MATLAB : Get smoothly coloured outputs when using surf / trisurf

I had been struggling since sometime to get smoothly textured outputs using surf / trisurf commands in MATLAB. The edges of the locally planar element being used by the respective commands used to always “stand out” with respect to the texture of the contained patch.¬† Only today i found out a simple 1 line command that solves this problem..

tri = delaunay(X,Y);

trisurf(tri,X,Y,Z);

shading interp;                   % other options are shading face / shading faceted

And you are done.. you can check for yourself the extent to which this 1 line of code creates a change in visualization of 3d textured data.

Sample outputs, before and after :

surf output BEFORE

surf output AFTER

MATLAB : Generate variable name and allot vector / matrix to it

Matlab provides a nice and simple way to generate continuous set of variable names, say in the form of var1, var2,… etc. This can be accomplished using genvarname and eval commands. Here i’ll show an example where we need to store columns of a matrix A in 3 vectors var1, var2 and var3. Following is the code snippet :

n = 3;                                         % say

A = [1 2 3;4 5 6;7 8 9];            %say

for i = 1:n

val = genvarname([‘var’ num2str(i)]);

eval([val ‘=A(:,i);’]);

end

And you are done.. the columns of A get stored in the vectors var1, var2 and var3. Similarly, even block matrices within A can be assigned this way. This is a much more elegant and faster way than doing the same thing via for-loops.