Search This Blog

Sunday, March 11, 2012

matlab to excel

Q) what is the excel equivalent of matlab's ismember function
A) vlookup

The syntax is vlookup(lookup_value, table_array, col_index_num, [range_look_up])

It can be used to check if a value exists in a column, compare two columns etc.,

The col_index_num starts from 1.
I usually give the 4th argument as false, so I can get an exact match. See help in excel for more details.

ISNA() - similar to matlab's isnan function, is another function that comes in handy with vlookup. It tells whether a number is #N/A or not.

Monday, February 27, 2012

computable document format

If you've got 20 minutes to spare, this is a great presentation to watch http://www.wolfram.com/broadcast/screencasts/announcing_cdf/ . Here Conrad Wolfram explains about a new file format that he calls "computable document format". IMHO, it is an excellent idea. One that could change the way we think about e-documents like pdf files, presentations, research publications etc.,

Monday, February 06, 2012

cygwin mintty vim output is not to a terminal

Inside the cygwin terminal if you do

$ uname -r
1.7.10(0.259/5/3)

$ mintty.exe

The mintty terminal pops up. However, if you run vim in the mintty terminal, there are warnings such as

$ vim junk1.txt
Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal

To get rid of these warnings, start mintty with "mintty.exe -". The .exe is optional. It is the '-' that is more important.

Why bother with the mintty terminal in the first place? Because it has a lot more functionality than the default cygwin terminal. For eg., the mintty terminal can be resized, maximized, minimized in both the directions; copy pasting text from/to the terminal is a breeze.

Sunday, December 25, 2011

verizon router dhcp getting a fixed IP address

I am currently using Verizon DSL to connect to the internet. So, I have

laptop -> router -> internet

A minor irritation with this set up is that the router can potentially assign different IP addresses whenever I do a reboot or something akin to /etc/init.d/networking restart. For example today it can be 192.168.1.58, tomorrow it could be 192.168.1.68 etc.,

I currently have

$ifconfig
eth0 Link encap:Ethernet HWaddr 00:15:c5:19:9c:1a
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:17

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6790 errors:0 dropped:0 overruns:0 frame:0
TX packets:6790 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:773105 (754.9 KiB) TX bytes:773105 (754.9 KiB)

wlan0 Link encap:Ethernet HWaddr 00:13:02:9e:cc:1b
inet addr:192.168.1.21 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::213:2ff:fe9e:cc1b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7486622 errors:0 dropped:0 overruns:0 frame:0
TX packets:4833561 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2650697959 (2.4 GiB) TX bytes:608573891 (580.3 MiB)

To have the same IP address, say 192.168.1.78, every time the computer is booted

1) Add the following lines to /etc/dhcp/dhclient.conf

alias {
interface "wlan0";
fixed-address 192.168.1.78;
option subnet-mask 255.255.255.255;
}

2) restart the network

sudo /etc/init.d/networking restart

Now you should see

$ifconfig
eth0 Link encap:Ethernet HWaddr 00:15:c5:19:9c:1a
UP BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
Interrupt:17

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6790 errors:0 dropped:0 overruns:0 frame:0
TX packets:6790 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:773105 (754.9 KiB) TX bytes:773105 (754.9 KiB)

wlan0 Link encap:Ethernet HWaddr 00:13:02:9e:cc:1b
inet addr:192.168.1.21 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::213:2ff:fe9e:cc1b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:7486678 errors:0 dropped:0 overruns:0 frame:0
TX packets:4833604 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:2650705479 (2.4 GiB) TX bytes:608589238 (580.3 MiB)

wlan0:0 Link encap:Ethernet HWaddr 00:13:02:9e:cc:1b
inet addr:192.168.1.78 Bcast:0.0.0.0 Mask:255.255.255.255
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
Note the new wlan:0 interface which is always assigned to 192.168.1.78.

Sunday, August 07, 2011

extract historical versions of a file tracked by git

Say, I have a file test.txt being tracked by git.
$git log test.txt
commit 809ebdbd3c44b9310a7a0f67d61897d5d8f9b6d2
Author: rajulocal
Date: Sun Aug 7 14:11:31 2011 -0400

version 15

commit aec45562ad09257ce85eaf00f5a4fa6912e9e51e
Author: rajulocal
Date: Sun Aug 7 14:11:21 2011 -0400

version 14

commit 7fb63da179a2026ba92a8875ff7d3da881ef5854
Author: rajulocal
Date: Sun Aug 7 14:11:11 2011 -0400

version 13

commit 9b3ca5d3d7497e44c012f5ccd5daced2dd90ec12
Author: rajulocal
Date: Sun Aug 7 14:10:51 2011 -0400

version 12

commit d0b5471fa2abce0057cce1d61240e78c09406c04
Author: rajulocal
Date: Sun Aug 7 14:10:40 2011 -0400

version 11

commit e3af4e1a85b4ff07c31c8dcda47eb41c3f3c830b
Author: rajulocal
Date: Sun Aug 7 14:10:26 2011 -0400

version 10

commit be762c0ee45ec6046fa960cace9acae513b80137
Author: rajulocal
Date: Sun Aug 7 14:10:18 2011 -0400

version 9

commit 03ad798434559300cff26f6a238d7b0f05ad53d2
Author: rajulocal
Date: Sun Aug 7 14:09:59 2011 -0400

version 8

commit 94c50cce3b24e572133eeaedc5bfe00aad568aec
Author: rajulocal
Date: Sun Aug 7 14:09:52 2011 -0400

version 7

commit ab63e2bc7b258f702cc8c516c1f16bafc754cf27
Author: rajulocal
Date: Sun Aug 7 14:09:44 2011 -0400

version 6

commit 16c896d19e00267b63537596485074488cd20a85
Author: rajulocal
Date: Sun Aug 7 14:09:37 2011 -0400

version 5

commit 6937331a12209121ba24d111e1ca5128a3c1e836
Author: rajulocal
Date: Sun Aug 7 14:09:21 2011 -0400

version 4

commit 7ec1c7f98a152f6b760ff1561a3b805cc5e374e9
Author: rajulocal
Date: Sun Aug 7 14:09:13 2011 -0400

version 3

commit 5b3788ad1bdef15c1895a6efefcdd1ea96c840e9
Author: rajulocal
Date: Sun Aug 7 14:09:01 2011 -0400

version 2

commit 1306cba057d6cac06ffd39b9234ce72967dc44f8
Author: rajulocal
Date: Sun Aug 7 14:08:35 2011 -0400

version 1


In order to extract the latest 12 versions of test.txt and store each into a different file, I do
$xtract_hist_git 12 test.txt
$ls m??_test.txt
m00_test.txt m02_test.txt m04_test.txt m06_test.txt m08_test.txt m10_test.txt m12_test.txt
m01_test.txt m03_test.txt m05_test.txt m07_test.txt m09_test.txt m11_test.txt

m00_test.txt is the latest copy of test.txt, m01_test.txt is the latest but one copy, m02_test.txt is the latest but two copy etc.,

xtract_hist_git is this little script
$cat ~/bin/xtract_hist_git           
#! /bin/sh
#
# Author : Kamaraju S. Kusumanchi
# Email : kamaraju at gmail dot com
# File : ~/bin/xtract_hist_git
# Date (last modified) : Sun Aug 7 14:28:14 EDT 2011

revs=$1
file=$2

for i in `seq -w 0 $revs`
do
git checkout master~$i $file
mv $file m${i}_$file
done
git checkout master $file

Tuesday, June 14, 2011

get all the odd lines in a file

sed can be used to get all the odd or even lines in a file. Consider, for example,

$cat linenums.txt
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10

To print all the odd lines

$sed -n '1~2p' linenums.txt
line 1
line 3
line 5
line 7
line 9

$sed -n '2~2p' linenums.txt
line 2
line 4
line 6
line 8
line 10

Saturday, February 26, 2011

login into a directory after sshing into a machine

To login into a machine via ssh and cd into a particular directory immediately afterwards, use
ssh -t user@machine.name 'cd ~/destination/directory && exec /bin/bash --login -i'
The -t option forces the ssh to open a pseudo-terminal. Without the -t option, ssh does not open the pseudo-terminal when a command is specified.

Commands can also contain variable names. For example,
export DYNAMIC=${PWD#$HOME/}
ssh -t user@machine.name "cd ~/$DYNAMIC && exec bash --login -i"
Other possible uses of the -t option

To see the output of top of a remote machine, use
ssh -t user@remote.machine.name top

Wednesday, February 02, 2011

Add a number to a variable in shell script

Consider the following shell script
$cat add_variables_02.sh
#! /bin/sh
ov=10;
nv=$((ov+3)); echo $nv
((nv=ov+3)); echo $nv
nv=$ov+3; echo $nv
nv=`expr $ov+3`; echo $nv
nv=`expr $ov + 3`; echo $nv
Execute in a shell
$bash add_variables_02.sh 
13
13
10+3
10+3
13
The first two methods nv=$((ov+3)) or ((nv=ov+3)) are faster than the fifth method nv=`expr $ov + 3` as the former uses bash's own feature and the later runs an external command in a subprocess.

Also, note that spaces are needed before and after the '+' character when expr is used.

Tested in Debian Wheezy machine using bash 4.2-1

Further reading
  1. info coreutils 'expr invocation'
  2. man expr

Special thanks to the anon 2011-08-24 5:06 AM for providing the first two methods.

Saturday, August 21, 2010

recover an accidentally overwritten partition table

shorter version:

Q. I accidentally overwrote the partition table on my computer. Now it does not boot. Is there any way to restore it back?
A. use the testdisk program. Debian users can install it by doing

# apt-get install testdisk

To run it, use the command

# testdisk

This tool is very powerful, easy to use and interactive. It can be run from a live CD. I used it to recover the ext3, Linux swap partition information.


Long story:

My favorite tool for partitioning is qtparted. I got hooked to it couple of years back when I was trying out a knoppix live CD.

The hard drive of my laptop is /dev/sda. I recently bought a 500 GB external USB hard drive. When I connected the USB hard drive, it was recognized as /dev/sdb. I used the qtparted program to partition the /dev/sdb.



The partitioning process went smoothly. But when I click on the /dev/sdb link in the left tab of qtparted window, there were error messages on my konsole

$sudo qtparted
No Implementation: Support for opening ntfs file systems is not implemented yet.
Error: File system has an incompatible feature enabled. Compatible features are has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or debugfs to remove features.
Error: File system has an incompatible feature enabled. Compatible features are has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or debugfs to remove features.

Not sure of what it means, I resorted to cfdisk (a Curses based tool for partitioning).


My intention was to delete all the partitions on /dev/sdb and start afresh. However, I ended up deleting all the ext3, swap partitions on /dev/sda by accident. Notice how cfdisk only lists /dev/sda and nothing about /dev/sdb?

When the changes are committed, it gave an error. It asked me to reboot the machine in order to fix the error.

When the laptop is rebooted, Grub does not load. IIRC, the error code was 17. My computer is now officially in a half baked state with the wrong partition table (all because of my stupidity).

Kumar Appiah suggested me to try testdisk. He used it before to recover some FAT32, NTFS partitions.

So, I popped in an old Ubuntu (hoary) live CD to see if it has testdisk. It does not. The Hoary was released a while back. The official repositories were removed. So a simple

# sudo apt-get update

did not work. I modifed the /etc/apt/sources.list to point to dapper instead of hoary. Then did an apt-get update, apt-get install testdisk.

In order to install testdisk from dapper onto hoary, apt-get wanted to install a new kernal and remove the current kernel. I let the upgrade process run and gave it a 'no' when it wanted to remove the current kernel during the post configure stage. This exited the apt-get process abruptly.

Then, I did a "sudo apt-get -f install" to fix the inconsistent state. I then had to do another "sudo apt-get install testdisk". This time it proceeded without any difficulty.

Once testdisk is installed in the RAM, running it was easy. Just execute

# testdisk

The instructions in testdisk screens are very clear. At the end, I was able to recover my original partition table. Kudos to the authors, Debian maintainers of testdisk and to Kumar for suggesting it.


Useful links:
. websites of testdisk , qtparted
. links to debian packages testdisk, qtparted, e2fsprogs

Tuesday, June 15, 2010

options in function variables octave

The following octave code shows how to make functions take different paths depending on a user specified choice. It is a little template that comes in handy once in a while.
$cat options_in_function_variables_01.m
# Author : Kamaraju S. Kusumanchi
# Email : kamaraju at gmail dot com
# Last modified : Tue Jun 15 06:45:15 EDT 2010
function [X] = options_in_function_variables_01(X, choice)

if (strcmp(choice, "square"))
X = X*X;
elseif (strcmp(choice, "unity"))
X = X*1;
elseif (strcmp(choice, "cube"))
X = X*X*X;
else
printf("choice = %s is not a valid option.\n", choice);
error("please provide a correct choice");
endif
endfunction

To test it

$octave -qf
octave:1> options_in_function_variables_01(2.2, 'unity')
ans = 2.2000
octave:2> options_in_function_variables_01(2.2, 'square')
ans = 4.8400
octave:3> options_in_function_variables_01(2.2, 'cube')
ans = 10.648
octave:4> 10.648/4.84
ans = 2.2000
octave:5> 4.84/2.2
ans = 2.2000

Monday, May 17, 2010

passing functions as arguments in octave

It is possible to pass functions as arguments to another function in Octave. Consider the following sample code

$ls
cube.m dynamic.m script.m square.m

$cat square.m
function [sq] = square(x)
sq = x**2;
endfunction

$cat cube.m
function [cu] = cube(x)
cu = x**3;
endfunction

$cat dynamic.m
function [ ret ] = dynamic (fh, x)
# fh is a function name passed as a string
# ex:- b = dynamic("cube", a);
# fh can also be a handle
# handle = @square;
# b = dynamic(handle, a);
ret = feval(fh, x);
endfunction

$cat script.m
a=5.0

# bh = b obtained by using handle as arguments
handle=@square;
bh = dynamic( handle, a)

# bs = b obtained by using strings as arguments
bs = dynamic("square", a)

# ch = c obtained by using handle as arguments
handle=@cube;
ch = dynamic( handle, a)

# cs = c obtained by using strings as arguments
cs = dynamic("cube", a)


Run the script.m in octave

$octave3.2 -qf script.m

a = 5
bh = 25
bs = 25
ch = 125
cs = 125

Tested using Debian Lenny (stable), Octave 3.2.4

Further Reading:
1) The section on "Function handles, Inline Functions, and Anonymous Functions" in the octave manual http://www.gnu.org/software/octave/doc/interpreter/Function-Handles-Inline-Functions-and-Anonymous-Functions.html

2) http://rosettacode.org/wiki/Higher-order_functions explains how to do this kind of "calling functions from functions" in various programming languages

Sunday, April 25, 2010

load data file with header lines in octave

When a data file contains some header lines before the actual data matrix, octave's dlmread function can be used to read the matrix of data and ignore the headers.

For example,

$cat input_data.m
col1 col2 col3
1, 2, 3
4, 5, 6
7, 8, 9
10, 11, 12

This file contains one header line and a 4x3 data matrix. To read it into octave, create a script called

$cat read_data.m
1;
A = dlmread("input_data.m", SEP=',', R0=1, C0=0);
A

$octave3.2 -qf
octave3.2:1> read_data
A =

1 2 3
4 5 6
7 8 9
10 11 12


For more information on dlmread function, use 'help dlmread' inside octave.

Tested using octave 3.2.4-3 on Debian stable (Lenny).

Wednesday, September 09, 2009

print a file by line numbers

Consider the file

$ cat name.txt
1 k
2 ka
3 kam
4 kama
5 kamar
6 kamara
7 kamaraj
8 kamaraju
9 kamaraju
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi

To print the contents of lines between 5 and 8

$ sed -n '5,8p' name.txt
5 kamar
6 kamara
7 kamaraj
8 kamaraju

$echo $?
0

To print the lines 10 to 20, just do

$ sed -n '10,20p' name.txt
10 kamaraju k
11 kamaraju ku
12 kamaraju kus
13 kamaraju kusu
14 kamaraju kusum
15 kamaraju kusuma
16 kamaraju kusuman
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi

$echo $?
0

Note that this command is successful even though the specified right limit (20) is greater than the number of lines in the file (19).

To print the nth line of a file (w.l.g. say n=8), just do

$ sed -n 8p name.txt
8 kamaraju

$ echo $?
0

End of line is represented by $. So, to print from 17th line till the end of the file, do

$ sed -n '17,$p' name.txt
17 kamaraju kusumanc
18 kamaraju kusumanch
19 kamaraju kusumanchi

$ echo $?
0

tags | print specific lines in a file

Saturday, June 20, 2009

No theme index file.dpkg error

While trying to upgrade to texmacs 1:1.0.7.2-1in Sid (unstable) on a machine running Debian Lenny (Stable), I was getting the following error

$sudo apt-get install texmacs
// bunch of apt-get messages

Setting up texmacs-common (1:1.0.7.2-1) ...
gtk-update-icon-cache: No theme index file.dpkg: error processing texmacs-common (--configure):
subprocess post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of texmacs:
texmacs depends on texmacs-common (= 1:1.0.7.2-1); however:
Package texmacs-common is not configured yet.
dpkg: error processing texmacs (--configure):
dependency problems - leaving unconfigured
Errors were encountered while processing:
texmacs-common
texmacs
E: Sub-process /usr/bin/dpkg returned an error code (1)


Install the gnome-icon-theme package to solve this bug.
$sudo apt-get install -t stable gnome-icon-theme
//bunch of apt-get messages

Selecting previously deselected package gnome-icon-theme.
(Reading database ... 225097 files and directories currently installed.)
Unpacking gnome-icon-theme (from .../gnome-icon-theme_2.22.0-1_all.deb) ...
Setting up texmacs-common (1:1.0.7.2-1) ...
gtk-update-icon-cache: Cache file created successfully.
Setting up texmacs (1:1.0.7.2-1) ...
Setting up gnome-icon-theme (2.22.0-1) ...

Thursday, June 04, 2009

running external commands

  1. To run external commands while editing a file in vim, use the '!' in the normal mode. For example
    :!ls -al
    will list the files
    :!date
    will display the current date.

  2. To read the output of external commands into the current file, do
    :r !date
    All the commands are run in normal mode. Press ESC key to enter the normal mode in vim.

    Further reading:- :help :!

  3. To run external commands in octave, use the system command. Sample octave session looks as
    $octave -q
    octave:1> system("date")
    Thu Jun 4 23:49:23 EDT 2009
    ans = 0
    octave:2> [ret_code output] = system("date");
    octave:3> ret_code
    ret_code = 0
    octave:4> output
    output = Thu Jun 4 23:49:40 EDT 2009

    octave:5> exit
    Further reading :- "doc system" shows the relevant help pages in octave.

  4. To run external commands in Fortran 90 programs, use the system command. Sample code looks as below
    $cat system.f90
    program callsystem
    implicit none
    !to examine the behaviour of the system command
    character (len=100)::cmd
    cmd="echo Wake up Neo"
    !if u are using ifc compiler use -Vaxlib during compilation
    call system(cmd//achar(0))
    call system(cmd)
    call system("date")
    ! The next line also works.
    ! call system("ls")
    end program callsystem

    $gfortran system.f90

    $./a.out
    Wake up Neo
    Wake up Neo
    Thu Jun 4 23:55:07 EDT 2009
  5. To run the external commands in C, use the system command available in stdlib.h. Sample code will be
    $cat system.c
    #include stdio.h
    #include stdlib.h

    int main() {
    /* Fixme :- <, > in the header files are not showing up on blogspot */
    int ret_code;
    ret_code = system("date");

    printf("%d\n", ret_code);
    return 0;
    }

    $gcc -Wall system.c

    $./a.out
    Fri Jun 5 00:04:14 EDT 2009
    0

    Further reading :- man system

    All the above are tested in Debian Lenny using vim 7.1, octave 3.0.1, gfortran 4.3.1, gcc 4.3.1

Followers