Forum Navigation: Select WHO'S ON: 09:27 PM General Forum Technical Forum Economics Forum Numerical Methods Forum Trading Forum The Quantitative Finance FAQs Proje... Student Forum Book And Research Paper Forum Programming and Software Forum The Quantitative Finance Code Libra... Careers Forum Events Board Brainteaser Forum Off Topic Forum and Website Bugs and Suggesti...

 new topic
 search
 bookshop
 jobs board
 news
 help
 file share
 audio visual
 articles
 blogs
 wiki
 forums
 home

 FORUMS > Programming and Software Forum < refresh >
 Topic Title: Matlab interview questions Created On Mon Jul 16, 07 06:01 PM Topic View: Branch View Threaded (All Messages) Threaded (Single Messages) Linear

 Pages: [ 1 2 >> Next ]

circumfly
Member

Posts: 23
Joined: Oct 2006

Mon Jul 16, 07 06:01 PM

Dear all,

Can anybody kindly give me some information on how to prepare the interview
questions on Matlab? I only
use Matlab to do some simple modelling work ( M files) in my PhD and not an
expert at all. Please give me
some sample questions if possible and then I can have a clearer
understanding of the requirements.

BTW, I going to interview with a Statistical Arbitrage firm which uses
Matlab to do most of the modelling
work and now I am in the second-round stage. So Matlab in stats arb
applications is my most interested
area.

Cheers,

-------------------------
Simply the best

misiti3780
Member

Posts: 149
Joined: Feb 2006

Mon Jul 16, 07 07:36 PM

i also would be interested in these questions, i have never seen a list of any matlab questions on these boards

msperlin
Senior Member

Posts: 608
Joined: Jul 2006

Mon Jul 16, 07 09:17 PM

Circumfly,

Besides the basic stuff, I think you should study:

- Use of optimizations functions (fmincon.m among others)

* how to use it
* how to use constraints in param vector with confuneq.m
* how to set up an econometric model (what kind of m files you need)
* how to pass aditional parameters to objective function without using globals

- Vectorization

* What is that ? How to do it ?
* Thinking is terms of full matrices/vectors and also some related functions for that
* Use of filter(), which works beautifully for some time series models

That's the main questions I would study/ask ...

-------------------------
My personal site with Matlab Code and research papers here

ZmeiGorynych
Senior Member

Posts: 875
Joined: Jul 2005

Tue Jul 17, 07 08:52 AM

Vectorized thinking is the absolute basics, things like b(b>1)=a(b>1).*c instead of writing a for loop.

Understanding what all the bits in the Statistical Toolbox actually do might be a good idea.

Also anonymous function handles, cell arrays, struct arrays and Java integration are in my opinion something you really should understand well if you profess to be any good at matlab.

Do you understand what the following code does, without having to think about it? You should.

x=x0(x0>=1);
edges=prctiles(x, 0:10:100);
[dummy bin]=histc(x, prctiles);
y=grpstats(x, bin, {@mean, @(x) x.*x-sqrt(x)});

this code also has a minor issue - can you spot it?

-------------------------
You give a hungry man a fish, he owes you one fish. You teach him to fish, you lose your monopoly on fishing.

Edited: Tue Jul 17, 07 at 08:58 AM by ZmeiGorynych

zeta
Senior Member

Posts: 1952
Joined: Sep 2005

Tue Jul 17, 07 05:29 PM

here's a brainteaser, something I'm trying to vectorize right now I want to detect the indicies in a vector where the 1/0 state changes occur, for a pattern recog task.

for i=2:length(ai)

if(((ai(i-1)==0)&(ai(i)==1))|((ai(i-1)==1)&(ai(i)==0)))

d(k)=i;
k++;
end

end

-------------------------
Sometimes I Doubt Your Commitment to Sparkle Motion

msperlin
Senior Member

Posts: 608
Joined: Jul 2006

Tue Jul 17, 07 06:17 PM

try this (i'm also adding a rand example vector):

y=randn(50,1)>0;

y1=y(1:end-1);
y2=y(2:end);

yT=y1+y2;

[idx]=find(yT==1);

for i=1:length(idx)
fprintf(1,['State transition between observations ',num2str(idx(i)),' and ',num2str(num2str(idx(i)+1)),'\n'])
end

It should do the trick...

-------------------------
My personal site with Matlab Code and research papers here

zeta
Senior Member

Posts: 1952
Joined: Sep 2005

Tue Jul 17, 07 08:19 PM

brilliant, thanks! nice use of find

to the originator, the other dsp functions are useful to know eg., fft, fftshift, ifft etc in addition to the aforementioned filter(). I'm not sure what's in the statistical toolbox, but you should have a handle on MLE/MCMC etc

-------------------------
Sometimes I Doubt Your Commitment to Sparkle Motion

msperlin
Senior Member

Posts: 608
Joined: Jul 2006

Tue Jul 17, 07 08:44 PM

I would also add repmat() into the study, which is specially good for creating matrices with a clear pattern in the order of elements, avoiding loops.

-------------------------
My personal site with Matlab Code and research papers here

zeta
Senior Member

Posts: 1952
Joined: Sep 2005

Tue Jul 17, 07 08:58 PM

also in the vein of vectorization, the following are useful: flipud, fliplr, flipdim and reshape

-------------------------
Sometimes I Doubt Your Commitment to Sparkle Motion

ZmeiGorynych
Senior Member

Posts: 875
Joined: Jul 2005

Wed Jul 18, 07 09:48 AM

Quote

Originally posted by: zeta
here's a brainteaser, something I'm trying to vectorize right now I want to detect the indicies in a vector where the 1/0 state changes occur, for a pattern recog task.

for i=2:length(ai)

if(((ai(i-1)==0)&(ai(i)==1))|((ai(i-1)==1)&(ai(i)==0)))

d(k)=i;
k++;
end

end

find( ( 2*ai(1: (end-1)) - 1).* ( 2*ai(2:end) - 1)<0);

I assume all entries are either 1 or 0.

Ah, just saw msperlin's method is similar but prettier

-------------------------
You give a hungry man a fish, he owes you one fish. You teach him to fish, you lose your monopoly on fishing.

Edited: Wed Jul 18, 07 at 09:50 AM by ZmeiGorynych

circumfly
Member

Posts: 23
Joined: Oct 2006

Wed Jul 18, 07 02:15 PM

Thanks everyone for your kind help . I have been trying my best to study and practise.

1. As far as Zeta's brainteaser is concerned, I proposed a vectorized code as follows:

index=find(filter([1,1],1,ai)==1)+1

Because for a change of 0/1 or 1/0, the sum of the two adjacent elements should equal 1. Am I right?

2. For ZmeiGorynych's question:

x=x0(x0>=1);
edges=prctiles(x, 0:10:100);
[dummy bin]=histc(x, prctiles);
y=grpstats(x, bin, {@mean, @(x) x.*x-sqrt(x)});

this code also has a minor issue - can you spot it?

I have no idea..... what a shame....What is @? file handles?..... ZmeiGorynych,please give the answer or hints to me. Thanks.

-------------------------
Simply the best

ZmeiGorynych
Senior Member

Posts: 875
Joined: Jul 2005

Wed Jul 18, 07 05:29 PM

@ are function handles. The rest should be easy enough to figure out using matlab help.

-------------------------
You give a hungry man a fish, he owes you one fish. You teach him to fish, you lose your monopoly on fishing.

Edited: Wed Jul 18, 07 at 05:29 PM by ZmeiGorynych

msperlin
Senior Member

Posts: 608
Joined: Jul 2006

Wed Jul 18, 07 06:14 PM

Quote

Originally posted by: circumfly
Thanks everyone for your kind help . I have been trying my best to study and practise.

1. As far as Zeta's brainteaser is concerned, I proposed a vectorized code as follows:

index=find(filter([1,1],1,ai)==1)+1

Circunfly, the idea for your proposal is right, but the index is wrong. The filter() sums up to t, so the t value in the vector from filter([1,1],1,ai) is with the sum from t-1 and t. This means that the regime is switching between t and t-1 (no need for +1 part).
The right would be:

index=find(filter([1,1],1,ai)==1) % change i between index(i) and index(i)-1

Also, Circunfly, yo have at least 3 different solutions for the brainteaser, why don't you create a script for checking the speed for each in a large scale application (hint: use random numbers)).

-------------------------
My personal site with Matlab Code and research papers here

zeta
Senior Member

Posts: 1952
Joined: Sep 2005

Wed Jul 18, 07 06:37 PM

thanks everybody for the elegant solutions

another handy dandy aspect of Matlab/Octave (and hence something worth asking/knowing for interviews) are the lovely string functions eg., here used to average outputs of a batching process:

for i=1:N
file =strrep("output.testing.1","1",num2str(i-1));
end

a=a./N;

-------------------------
Sometimes I Doubt Your Commitment to Sparkle Motion

zeta
Senior Member

Posts: 1952
Joined: Sep 2005

Wed Jul 18, 07 06:40 PM

or the use of hist for thresholding eg., image binarization

[m,n]=size(b);
[d e]=hist(reshape(b,1,n*m),[1:256]);
[i j]=max(d);

f=zeros(m,n);
f=(b!=e(j));

-------------------------
Sometimes I Doubt Your Commitment to Sparkle Motion

ZmeiGorynych
Senior Member

Posts: 875
Joined: Jul 2005

Fri Jul 20, 07 09:26 AM

Quote

x=x0(x0>=1);
edges=prctiles(x, 0:10:100);
[dummy bin]=histc(x, prctiles);
y=grpstats(x, bin, {@mean, @(x) x.*x-sqrt(x)});

this code also has a minor issue - can you spot it?

I have no idea..... what a shame....What is @? file handles?..... ZmeiGorynych,please give the answer or hints to me. Thanks.

Well, the first line just throws away negative and NaN values in x.
The next 3 lines compute two summary statistics by decile: the mean of each decile and a weird summary function that I just made up.
First line gets the values of each decile, second line assigns to each point the number of the decile it's in, and the last line does summary statistics grouping by that number.

Now can you spot the big problem and the minor problem with this code?

-------------------------
You give a hungry man a fish, he owes you one fish. You teach him to fish, you lose your monopoly on fishing.

circumfly
Member

Posts: 23
Joined: Oct 2006

Sat Jul 21, 07 12:43 PM

Hi all,

I got a new question for you guys:

Players 1,2,3.....,n are seated around a table and each has a single penny. Player 1 passes a penny to Player 2, who then passes two pennies to Player 3. Player 3 then passes one penny to Player 4, who passes two pennies to Player 5, and so on, players alternately passing one penny or two to the next player who still has some pennies. A player who runs out of pennies drops out of the game and leave the table. Define X as the (infinite) set of numbers n for which some player ends up with all n pennies.

I implemented a straightforward algorithm (you can see it below) but not very elegant. It takes more than 15 minutes to find the elements of X under 500. So can you guys propose some strategies to make it faster? Hopefully by some kind of vectorization.

PS: my Matlab code:

x=[1:500];
logical_x=zeros(1,500);

for ii=1:500
penny=ones(1,x(ii));
penny_last_round=ones(1,x(ii)); % this variable stores the state of the number of pennies in the last play.
num_rounds=0;
index=1;

while((length(penny)>1)&&(num_rounds<=1e4))

while(index<=(length(penny)-1))

if((penny(index)-penny_last_round(index))==1)
penny_last_round(1:length(penny))=penny(;
penny(index)=penny(index)-2;
penny(index+1)=penny(index+1)+2;
else
penny_last_round(1:length(penny))=penny(;
penny(index)=penny(index)-1;
penny(index+1)=penny(index+1)+1;
end;

index=index+1;

end; % end of the inner while loop

penny=penny(find(penny)); % remove 0s from the array----players with no penny in hand drop the game
penny_last_round=penny_last_round(find(penny_last_round));
if(length(penny)==1)
break;
end;

if(penny(end)-penny_last_round(length(penny))==1)
penny_last_round(1:length(penny))=penny(;
penny(1)=penny(1)+2;
penny(end)=penny(end)-2;
else
penny_last_round(1:length(penny))=penny(;
penny(1)=penny(1)+1;
penny(end)=penny(end)-1;
end;

penny=penny(find(penny));
index=1;
num_rounds=num_rounds+1;

end; % end of the outer while loop.

if(length(penny)==1)
logical_x(ii)=1;
end;

end; % end of the for loop;

x=x(find(logical_x));
disp(x);

-------------------------
Simply the best

msperlin
Senior Member

Posts: 608
Joined: Jul 2006

Sun Jul 22, 07 04:12 AM

Quote

Originally posted by: circumfly
Hi all,

I got a new question for you guys:

Players 1,2,3.....,n are seated around a table and each has a single penny. Player 1 passes a penny to Player 2, who then passes two pennies to Player 3. Player 3 then passes one penny to Player 4, who passes two pennies to Player 5, and so on, players alternately passing one penny or two to the next player who still has some pennies. A player who runs out of pennies drops out of the game and leave the table. Define X as the (infinite) set of numbers n for which some player ends up with all n pennies.

I implemented a straightforward algorithm (you can see it below) but not very elegant. It takes more than 15 minutes to find the elements of X under 500. So can you guys propose some strategies to make it faster? Hopefully by some kind of vectorization.

Circunfly, I took a quick look into it but didn't find any place for vectorization.

The main engine in the code is the recursion loop/while for computing the pennies.
I don't other way around it besides the use of the loops.

Btw, did you previously programmed in Phyton ?

I'm starting to studying other programming languages and the way you coded the for loop is very similar...

-------------------------
My personal site with Matlab Code and research papers here

circumfly
Member

Posts: 23
Joined: Oct 2006

Sun Jul 22, 07 03:26 PM

msperlin, actually I only programmed with Fortran, C++ and Matlab, do not know what Phyton is at all...

-------------------------
Simply the best

msperlin
Senior Member

Posts: 608
Joined: Jul 2006

Sun Jul 22, 07 07:43 PM

Ok, sorry then. But it does look similar to phyton...

-------------------------
My personal site with Matlab Code and research papers here