Revision 7d0210ea-cf11-4b64-bdbc-b4c244c0b2d9 - Code Golf Stack Exchange

# MATLAB, <strike>146</strike> <strike>143</strike> 138 #

(Also works on Octave online, but you need to sign in to save the function in a file).

 function o=c(n,L);o=zeros(n);o(:,1)=1;for i=2:n;for j=2:i;a=o(i-1,j-1);b=o(i-1,j);c=a|b;d=a&b;c(d)=L(d);L=circshift(L,-d);o(i,j)=c;end;end

The function takes an input ```n``` and ```L```, and returns an array ```o``` which contains the output. 

For the input values, ```n``` is a scalar, and ```L``` is a column vector, which can be specified in the format ```[;;;]```. Not quite what you show, but you say it is flexible within reason and this seems so.

The output is formatted as an ```n x n``` array containing 0's and 1's.

And an explanation:

 function o=c(n,L)
 %Create the initial array - an n x n square with the first column made of 1's
 o=zeros(n);o(:,1)=1;
 %For each row (starting with the second, as the first is done already)
 for i=2:n;
 %For each column in that row, again starting with the second as the first is done
 for j=2:i;
 %Extract the current and previous elements in the row above
 a=o(i-1,j-1); %(previous)
 b=o(i-1,j); %(current)
 %Assume that min()==0, so set c to max();
 c=a|b;
 %Now check if min()==1
 d=a&b;
 %If so, set c to L(1)
 c(d)=L(d);
 %Rotate L around only if min()==1
 L=circshift(L,-d);
 %And store c back to the output matrix
 o(i,j)=c;
 end;
 end

---

Update: I have managed to optimise away the if-else statement to save a few bytes. The input format has once again changed back to column vector.

AltStyle によって変換されたページ (->オリジナル) /