Monday, May 21, 2012

Vertical xtick Labels in Matlab graphs

This was taken from the mailing list entry available at: https://mailman.cae.wisc.edu/pipermail/help-octave/2009-July/036066.html
--------------------------------

here is a script that illustrates how to rotate the xtick label and put them vertically.

clear
## init demo plot
clf
plot(1:4);
xtick=[1 2 3 4];
set(gca,'xtick',xtick);
xticklabel=["a";"b";"c";"d"];
set(gca,'xticklabel',xticklabel);

## get position of current xtick labels
h = get(gca,'xlabel');
xlabelstring = get(h,'string');
xlabelposition = get(h,'position');

## construct position of new xtick labels
yposition = xlabelposition(2);
yposition = repmat(yposition,length(xtick),1);

## disable current xtick labels
set(gca,'xtick',[]);

## set up new xtick labels and rotate
hnew = text(xtick, yposition, xticklabel);
set(hnew,'rotation',90,'horizontalalignment','right');

Saturday, May 19, 2012

Xor Range without LOOP

I am lazy, and I wont be able to explain it more clearly than him. Hence, I lifted the content directly from there, to have all the coding tricks in my blog for my reference. 
----------------------
HOW to get Xor between numbers from 1 to 300000000
you will get TLE if you try to use Loop for all numbers
Question was asked on the codeforces site. Click Here to see the discussion 

jacob answer
Let's introduce
 f(x) = x ^ (x-1) ^ (x-2) ^ ... ^ 1
Then anwer would be  
f(end) ^ f(start - 1)


Let's now learn to calculate f(x) fast enough. First, notice that f(4*k - 1) = 0 (provable by induction). So to calculate f(x) you only need to take at most 4 last numbers and xor them.
Finally,
f(4*k) = 4*k
f(4*k + 1) = 1
f(4*k + 2) = 4*k + 3
f(4*k + 3) = 0 


Expressions "at most 4 last numbers" and "only last 4 numbers" are different. In fact, you need to take exactly (x+1)%4 last numbers.

Conclusion:Use this function 


int f(int n){  
switch(n%4){  
case 0: return n;  
case 1: return 1;  
case 2: return n+1;  
default: return 0;  
}  
}