Hi, I have a matrix
B = 0 9 7 0 0 0 9 7 6 0 0 2 7 6 0 8 0 4 2 0
rows represent time series and columns represent assets. In each row ( time) I need to get a strategy which allocates 1 equally to assets that do not equal zero. The results for the strategy s should =[0 1/2 1/2 0 0; 0 1/3 1/3 1/3 0;0 1/3 1/3 1/3 0;1/3 0 1/3 1/3 0]. My code is as follows but I am looking for a more efficient solution. Thank you.
weight=zeros(4,5); for r=1:4 countzero=0; index=[]; for c=1:5 if B(r,c)==0 weight(r,c)=0; else countzero=countzero+1; index=[index,c]; end end weight(r,index)=1/countzero; end
Best Answer