Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

Commonmark migration
Source Link

#MATLAB, (削除) 146 (削除ここまで)(削除) 143 (削除ここまで) 138#

MATLAB, (削除) 146 (削除ここまで)(削除) 143 (削除ここまで) 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.

#MATLAB, (削除) 146 (削除ここまで)(削除) 143 (削除ここまで) 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.

MATLAB, (削除) 146 (削除ここまで)(削除) 143 (削除ここまで) 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.

added 132 characters in body
Source Link
Tom Carpenter
  • 4.3k
  • 14
  • 21

#MATLAB, (削除) 146 (削除ここまで) 143#(削除) 143 (削除ここまで) 138#

function o=c(n,L);o=zeros(n);o(:,1)=1;for i=2:n forn;for j=2:i a=oi;a=o(i-1,j-1);b=o(i-1,j);if;c=a|b;d=a&b;c(a&bd) c=L=L(1d);L=L;L=circshift([2:end 1]L,-d);else c=a|b;end;o;o(i,j)=c;end;end

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

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:nn;
 %For each column in that row, again starting with the second as the first is done
 for j=2:ii;
 %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 ifmin(a&b)==0, so set c to c=Lmax(1); %If they are both 1 (min>0), select the next random seedc=a|b;
 %Now check if L=Lmin([2:end 1]); %Rotate the random seed==1
 so we get the next element in itd=a&b;
 %If so, set c to elseL(1)
 c(d)=L(d);
 c=a|b;%Rotate L around only %Otherwiseif theymin()==1
 are different, so set to the max valueL=circshift(L,-d);
 %And store c end
back to the output matrix
 o(i,j)=c; %Store in the current column of the current row
 endend;
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.

#MATLAB, (削除) 146 (削除ここまで) 143#

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);if(a&b) c=L(1);L=L([2:end 1]);else c=a|b;end;o(i,j)=c;end;end

For the input values, n is a scalar, and L is a row vector (which can be specified in the exact format you show [,,,]).

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)
 if(a&b) c=L(1); %If they are both 1 (min>0), select the next random seed
 L=L([2:end 1]); %Rotate the random seed so we get the next element in it
 else
 c=a|b; %Otherwise they are different, so set to the max value
 end
 o(i,j)=c; %Store in the current column of the current row
 end
end

#MATLAB, (削除) 146 (削除ここまで) (削除) 143 (削除ここまで) 138#

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

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.

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.

Shave off a few bytes and reformat the input
Source Link
Tom Carpenter
  • 4.3k
  • 14
  • 21

#MATLAB, 146#(削除) 146 (削除ここまで) 143#

(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);if(a&b) c=L(1);L=circshift;L=L(L,-1[2:end 1]);else c=a|b;end;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 columnrow vector. As you said the input format isn't strict (within reason), when calling the function L shouldwhich can be written for example as:specified in the exact format you show [0;0;1][,,,] to ensure the correct orientation of the vector).

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)
 if(a&b)
 c=L(1); %If they are both 1 (min>0), select the next random seed
 L=circshiftL=L(L,-1[2:end 1]); %Rotate the random seed so we get the next element in it
 else
 c=a|b; %Otherwise they are different, so set to the max value
 end
 o(i,j)=c; %Store in the current column of the current row
 end
 end

#MATLAB, 146#

(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);if(a&b) c=L(1);L=circshift(L,-1);else c=a|b;end;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. As you said the input format isn't strict (within reason), when calling the function L should be written for example as: [0;0;1] to ensure the correct orientation of the vector.

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)
 if(a&b)
 c=L(1); %If they are both 1 (min>0), select the next random seed
 L=circshift(L,-1); %Rotate the random seed so we get the next element in it
 else
 c=a|b; %Otherwise they are different, so set to the max value
 end
 o(i,j)=c; %Store in the current column of the current row
 end
 end

#MATLAB, (削除) 146 (削除ここまで) 143#

(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);if(a&b) c=L(1);L=L([2:end 1]);else c=a|b;end;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 row vector (which can be specified in the exact format you show [,,,]).

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)
 if(a&b)
 c=L(1); %If they are both 1 (min>0), select the next random seed
 L=L([2:end 1]); %Rotate the random seed so we get the next element in it
 else
 c=a|b; %Otherwise they are different, so set to the max value
 end
 o(i,j)=c; %Store in the current column of the current row
 end
 end
Source Link
Tom Carpenter
  • 4.3k
  • 14
  • 21
Loading

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