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

###Explanation

Explanation

###Explanation

Explanation

added 2274 characters in body
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

###Explanation

* % Take two inputs implicitly. Multiply
 % STACK: 16
: % Range
 % STACK: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
2G % Push second input again
 % STACK: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16], 4
e % Reshape with that number of rows, in column-major order
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16]
t % Duplicate
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16]
i= % Take third input and compare, element-wise
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [0 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 0]
&f % Row and column indices of nonzeros (1-based)
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], 2, 2,
h % Concatenate horizontally
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2]
3Y6 % Push Moore mask
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [1 1 1; 1 0 1; 1 1 1]
&f % Row and column indices of nonzeros (1-based)
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [1; 2; 3; 1; 3; 1; 2; 3], [1; 1; 1; 2; 2; 3; 3; 3] 
h % Concatenate horizontally
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [1 1; 2 1; 3 1; 1 2; 3 2; 1 3; 2 3; 3 3] 
2- % Subtract 2, element-wise
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [-1 -1; 0 -1; 1 -1; -1 0; -1 0; -1 1; 0 1; 1 1] 
+ % Add, element-wise with broadcast
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [1 1; 2 1; 3 1; 1 2; 3 2; 1 3; 2 3; 3 3]
! % Transpose
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [1 2 3 1 3 1 2 3; 1 1 1 2 2 3 3 3]
Z{ % Convert into a cell array of rows
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % {[1 2 3 1 3 1 2 3], [1 1 1 2 2 3 3 3]}
) % Index. A cell array acts as an element-wise (linear-like) index
 % STACK: [1 2 3 5 7 9 10 11]

###Explanation

* % Take two inputs implicitly. Multiply
 % STACK: 16
: % Range
 % STACK: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16]
2G % Push second input again
 % STACK: [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16], 4
e % Reshape with that number of rows, in column-major order
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16]
t % Duplicate
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16]
i= % Take third input and compare, element-wise
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [0 0 0 0; 0 1 0 0; 0 0 0 0; 0 0 0 0]
&f % Row and column indices of nonzeros (1-based)
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], 2, 2,
h % Concatenate horizontally
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2]
3Y6 % Push Moore mask
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [1 1 1; 1 0 1; 1 1 1]
&f % Row and column indices of nonzeros (1-based)
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [1; 2; 3; 1; 3; 1; 2; 3], [1; 1; 1; 2; 2; 3; 3; 3] 
h % Concatenate horizontally
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [1 1; 2 1; 3 1; 1 2; 3 2; 1 3; 2 3; 3 3] 
2- % Subtract 2, element-wise
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16], [2 2],
 % [-1 -1; 0 -1; 1 -1; -1 0; -1 0; -1 1; 0 1; 1 1] 
+ % Add, element-wise with broadcast
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [1 1; 2 1; 3 1; 1 2; 3 2; 1 3; 2 3; 3 3]
! % Transpose
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % [1 2 3 1 3 1 2 3; 1 1 1 2 2 3 3 3]
Z{ % Convert into a cell array of rows
 % STACK: [1 5 9 13; 2 6 10 14; 3 7 11 15; 4 8 12 16],
 % {[1 2 3 1 3 1 2 3], [1 1 1 2 2 3 3 3]}
) % Index. A cell array acts as an element-wise (linear-like) index
 % STACK: [1 2 3 5 7 9 10 11]
Source Link
Luis Mendo
  • 106.7k
  • 10
  • 139
  • 382

MATL, 24 bytes

*:2Geti=&fh3Y6&fh2-+!Z{)

Inputs are h, w, i. The output is a row vector or column vector with the numbers.

Input i and output are 1-based.

Try it online! Or verify all test cases.

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