Matrix with two diagonals: Difference between revisions
Content deleted Content added
m [Uiua] ⇌ works on the rows of the array by default, so ≡ isn't needed. |
|||
(41 intermediate revisions by 25 users not shown) | |||
Line 1:
{{Draft task|Matrices}}
;Task:
Line 5:
<br> If you can please use GUI
<br><br>
===See also===
* [[Four sides of square]]
* [[Mosaic matrix]]
=={{header|11l}}==
{{trans|Wren}}
<syntaxhighlight lang="11l">
F two_diagonal_matrix(n)
L(i) 0 .< n
L(j) 0 .< n
print(I i == j | i + j == n - 1 {1} E 0, end' ‘ ’)
print()
two_diagonal_matrix(6)
print()
two_diagonal_matrix(7)
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
1 0 0 0 0 0 1
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
1 0 0 0 0 0 1
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">
;;; draw a matrix with 1s on the diagonals and 0s elsewhere
;;; draws a matrix with height and width = n with 1s on the diagonals
PROC drawDiagonals( INT n )
CARD i, j, r
r = n
FOR i = 1 TO n DO
FOR j = 1 TO n DO
Put(' )
IF j = i OR j = r THEN Put('1) ELSE Put('0) FI
OD
PutE()
r ==- 1
OD
RETURN
PROC Main()
drawDiagonals( 6 )
PutE()
drawDiagonals( 7 )
RETURN
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
1 0 0 0 0 0 1
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
1 0 0 0 0 0 1
</pre>
=={{header|Ada}}==
<
with Ada.Command_Line;
Line 46 ⟶ 125:
Put_Line ("Usage: ./matrix_with_diagonals <side-length>");
end Matrix_With_Diagonals;</
=={{header|ALGOL 68}}==
<
# draws a matrix with height and width = n with 1s on the diagonals #
PROC draw 2 diagonals = ( INT n )VOID:
Line 68 ⟶ 147:
print( ( newline ) );
draw 2 diagonals( 11 )
END</
{{out}}
<pre>
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 1
</pre>
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">
begin % draw a matrix with 1s on the diagonals and 0s elsewhere %
% draws a matrix with height and width = n with 1s on the diagonals %
procedure draw2Diagonals ( integer value n ) ;
begin
integer lPos, rPos;
lPos := 1;
rPos := n;
for i := 1 until n do begin
for j := 1 until n do writeon( s_w := 0, if j = lPos or j = rPos then " 1" else " 0" );
write();
lPos := lPos + 1;
rPos := rPos - 1
end for_i
end draw2Diagonals ;
% test the draw 2 diagonals procedure %
draw2Diagonals( 10 );
write();
draw2Diagonals( 11 )
end.
</syntaxhighlight>
{{out}}
<pre>
Line 97 ⟶ 224:
=={{header|APL}}==
{{works with|Dyalog APL}}
<
twoDiagonals¨ 10 11</
{{out}}
<pre> 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1
Line 111 ⟶ 238:
1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 1 </pre>
=={{header|AppleScript}}==
===Procedural===
<
if (n < 2) then error "twoDiagonalMatrix() handler: parameter must be > 1."
Line 151 ⟶ 279:
return linefeed & matrixToText(twoDiagonalMatrix(7), space) & ¬
(linefeed & linefeed & matrixToText(twoDiagonalMatrix(8), space))</
{{output}}
<
1 0 0 0 0 0 1
0 1 0 0 0 1 0
Line 170 ⟶ 298:
0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1"</
===Functional===
<
-- bothDiagonals :: Int -> [[Int]]
Line 312 ⟶ 440:
set my text item delimiters to dlm
return s
end unwords</
{{Out}}
<pre>1 0 0 0 0 0 1
Line 330 ⟶ 458:
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">drawSquare: function [side][
loop 1..side 'x ->
print map 1..side 'y [
(any? @[x=y side=x+y-1])? -> 1 -> 0
]
]
drawSquare 6
print ""
drawSquare 9</syntaxhighlight>
{{out}}
<pre>1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">for i, v in [8, 9]
result .= "Matrix Size: " v "*" v "`n" matrix2txt(diagonalMatrix(v)) "`n"
MsgBox % result
return
diagonalMatrix(size){
M := []
loop % size {
row := A_Index
loop % size
M[row, A_Index] := (row = A_Index || row = size-A_Index+1) ? 1 : 0
}
return M
}
matrix2txt(M){
for row , obj in M {
for col, v in obj
result .= M[row, col] " "
result .= "`n"
}
return result
}</syntaxhighlight>
{{out}}
<pre>Matrix Size: 8*8
1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1
Matrix Size: 9*9
1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1 </pre>
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f MATRIX_WITH_TWO_DIAGONALS.AWK
BEGIN {
Line 347 ⟶ 552:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 365 ⟶ 570:
1 0 0 0 0 0 1
</pre>
=={{header|Basic}}==
<
110 REM DIAGONAL-DIAGONAL MATRIX
120 REM
Line 388 ⟶ 594:
290 NEXT I
300 END
</syntaxhighlight>
{{Output}}
<pre> 1 0 0 0 0 0 1
Line 398 ⟶ 604:
1 0 0 0 0 0 1
</pre>
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
let diagonals(size) be
for y = 1 to size
for x = 1 to size
do writef("%C%C",
x=y | size-x=y-1 -> '1', '0',
x=size -> '*N', ' ')
let start() be
$( diagonals(9)
wrch('*N')
diagonals(10)
$)</syntaxhighlight>
{{out}}
<pre>1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1</pre>
=={{header|BQN}}==
<syntaxhighlight lang="bqn">D2 ← ∨⟜⌽∾˜⥊+⟜1↑×
D2 7</syntaxhighlight>
{{out}}
<pre>┌─
╵ 1 0 0 0 0 0 1
0 1 0 0 0 1 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
0 0 1 0 1 0 0
0 1 0 0 0 1 0
1 0 0 0 0 0 1
┘</pre>
=={{header|C}}==
{{trans|Wren}}
<
void specialMatrix(unsigned int n) {
Line 422 ⟶ 680:
specialMatrix(11); // odd n
return 0;
}</
{{out}}
Line 451 ⟶ 709:
=={{header|C++}}==
<
#include <iostream>
Line 481 ⟶ 739:
}
</syntaxhighlight>
{{out}}
<pre> 1 0 0 0 0 0 0 1
Line 501 ⟶ 759:
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">matrix = array[array[int]]
diagonals = proc (size: int) returns (matrix)
mat: matrix := matrix$new()
for y: int in int$from_to(1, size) do
line: array[int] := array[int]$new()
for x: int in int$from_to(1, size) do
n: int
if x=y cor size-x=y-1
then n := 1
else n := 0
end
array[int]$addh(line, n)
end
matrix$addh(mat, line)
end
return(mat)
end diagonals
print_matrix = proc (s: stream, mat: matrix)
for line: array[int] in matrix$elements(mat) do
for elem: int in array[int]$elements(line) do
stream$puts(s, " " || int$unparse(elem))
end
stream$putl(s, "")
end
end print_matrix
start_up = proc ()
po: stream := stream$primary_output()
print_matrix(po, diagonals(9))
stream$putl(po, "")
print_matrix(po, diagonals(10))
end start_up</syntaxhighlight>
{{out}}
<pre> 1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
It seems to me you should actually build the matricies, not just draw a bunch of numbers on the screen. Consequently, this code actually builds general purpose matrices to solve the problem. Once again, notice how the code is modular, breaking the operations down into separate subroutines that can be reused in other situations. This is how you build libraries and simplify your code when working on larger problems.
<syntaxhighlight lang="Delphi">
{Define a matrix}
type TMatrix = array of array of double;
procedure DisplayMatrix(Memo: TMemo; Mat: TMatrix);
{Display specified matrix}
var X,Y: integer;
var S: string;
begin
S:='';
for Y:=0 to High(Mat) do
begin
S:=S+'[';
for X:=0 to High(Mat[0]) do
if X=0 then S:=S+Format('%0.0f',[Mat[X,Y]])
else S:=S+Format('%2.0f',[Mat[X,Y]]);
S:=S+']'+#$0D#$0A;
end;
Memo.Lines.Add(S);
end;
procedure ClearMatrix(var Mat: TMatrix; Value: double);
{Set all elements of the matrix to specified value}
var X,Y: integer;
begin
for Y:=0 to High(Mat) do
for X:=0 to High(Mat[0]) do Mat[X,Y]:=0;
end;
procedure SetMatrixDiagonals(var Mat: TMatrix);
{Set both diagonals to one}
var X,Y: integer;
begin
{Set diagonals to one}
for X:=0 to High(Mat) do Mat[X,X]:=1;
for X:=0 to High(Mat) do Mat[X,High(Mat)-X]:=1
end;
procedure BuildDiagionalMatrix(var Mat: TMatrix; Size: integer);
{Build a matrix with diagonals of the specified size }
var X,Y: integer;
begin
SetLength(Mat,Size,Size);
ClearMatrix(Mat,0);
SetMatrixDiagonals(Mat);
end;
procedure BuildAndShowMatrix(Memo: TMemo; var Mat: TMatrix; Size: integer);
{Build and show a matrix of specific size}
begin
Memo.Lines.Add(Format('Matrix = %2d X %2d',[Size,Size]));
BuildDiagionalMatrix(Mat,Size);
DisplayMatrix(Memo,Mat);
end;
procedure DisplayDiagonalMatrices(Memo: TMemo);
{Build and display matrices of various sizes}
var Mat: TMatrix;
var I: integer;
begin
for I:=3 to 10 do BuildAndShowMatrix(Memo,Mat,I);
end;
</syntaxhighlight>
{{out}}
<pre>
Matrix = 3 X 3
[1 0 1]
[0 1 0]
[1 0 1]
Matrix = 4 X 4
[1 0 0 1]
[0 1 1 0]
[0 1 1 0]
[1 0 0 1]
Matrix = 5 X 5
[1 0 0 0 1]
[0 1 0 1 0]
[0 0 1 0 0]
[0 1 0 1 0]
[1 0 0 0 1]
Matrix = 6 X 6
[1 0 0 0 0 1]
[0 1 0 0 1 0]
[0 0 1 1 0 0]
[0 0 1 1 0 0]
[0 1 0 0 1 0]
[1 0 0 0 0 1]
Matrix = 7 X 7
[1 0 0 0 0 0 1]
[0 1 0 0 0 1 0]
[0 0 1 0 1 0 0]
[0 0 0 1 0 0 0]
[0 0 1 0 1 0 0]
[0 1 0 0 0 1 0]
[1 0 0 0 0 0 1]
Matrix = 8 X 8
[1 0 0 0 0 0 0 1]
[0 1 0 0 0 0 1 0]
[0 0 1 0 0 1 0 0]
[0 0 0 1 1 0 0 0]
[0 0 0 1 1 0 0 0]
[0 0 1 0 0 1 0 0]
[0 1 0 0 0 0 1 0]
[1 0 0 0 0 0 0 1]
Matrix = 9 X 9
[1 0 0 0 0 0 0 0 1]
[0 1 0 0 0 0 0 1 0]
[0 0 1 0 0 0 1 0 0]
[0 0 0 1 0 1 0 0 0]
[0 0 0 0 1 0 0 0 0]
[0 0 0 1 0 1 0 0 0]
[0 0 1 0 0 0 1 0 0]
[0 1 0 0 0 0 0 1 0]
[1 0 0 0 0 0 0 0 1]
Matrix = 10 X 10
[1 0 0 0 0 0 0 0 0 1]
[0 1 0 0 0 0 0 0 1 0]
[0 0 1 0 0 0 0 1 0 0]
[0 0 0 1 0 0 1 0 0 0]
[0 0 0 0 1 1 0 0 0 0]
[0 0 0 0 1 1 0 0 0 0]
[0 0 0 1 0 0 1 0 0 0]
[0 0 1 0 0 0 0 1 0 0]
[0 1 0 0 0 0 0 0 1 0]
[1 0 0 0 0 0 0 0 0 1]
</pre>
=={{header|Draco}}==
<syntaxhighlight lang="draco">proc setDiagonals([*,*] byte matrix) void:
word x, y, width, height;
width := dim(matrix, 1);
height := dim(matrix, 2);
for x from 0 upto width-1 do
for y from 0 upto height-1 do
matrix[x,y] :=
if x = y or width - x - 1 = y
then 1
else 0
fi
od
od
corp
proc printMatrix([*,*] byte matrix) void:
word x, y, width, height;
width := dim(matrix, 1);
height := dim(matrix, 2);
for y from 0 upto height-1 do
for x from 0 upto width-1 do
write(' ', matrix[x,y]:1)
od;
writeln()
od
corp
proc main() void:
[9,9] byte m_odd;
[10,10] byte m_even;
setDiagonals(m_odd);
printMatrix(m_odd);
writeln();
setDiagonals(m_even);
printMatrix(m_even)
corp</syntaxhighlight>
{{out}}
<pre> 1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1</pre>
=={{header|EasyLang}}==
{{trans|Nim}}
<syntaxhighlight>
proc matrix side . .
for i to side
for j to side
if i = j or i = side - j + 1
write "1 "
else
write "0 "
.
.
print ""
.
.
matrix 6
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
</pre>
Line 513 ⟶ 1,061:
{{Works with|Office 365 betas 2021}}
<
=LAMBDA(n,
LET(
Line 525 ⟶ 1,073:
)
)
)</
{{Out}}
The formulae in cells A2 and B2 populate the whole of each adjacent matrix.
Line 736 ⟶ 1,284:
Binding the name ''bothDiagonalMatrix'' in the Excel Name Manager:
<
=LAMBDA(n,
MAKEARRAY(
Line 748 ⟶ 1,296:
)
)
)</
{{Out}}
{| class="wikitable"
Line 955 ⟶ 1,503:
=={{header|F_Sharp|F#}}==
<
// Matrix with two diagonals. Nigel Galloway: February 17th., 2022
let m11 m=Array2D.init m m (fun n g->if n=g || n+g=m-1 then 1 else 0)
printfn "%A\n\n%A" (m11 5) (m11 6)
</syntaxhighlight>
{{out}}
<pre>
Line 978 ⟶ 1,526:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
: <x-matrix> ( n -- matrix )
Line 984 ⟶ 1,532:
6 <x-matrix> simple-table. nl
7 <x-matrix> simple-table.</
{{out}}
<pre>
Line 1,002 ⟶ 1,550:
1 0 0 0 0 0 1
</pre>
=={{header|FreeBASIC}}==
===Text based===
<
For i As Integer = 1 To n
For j As Integer = 1 To n
Line 1,018 ⟶ 1,565:
Print
twoDiagonalMatrix(7)
Sleep</
{{out}}
<pre>1 0 0 0 0 1
Line 1,036 ⟶ 1,583:
===Graphical===
<
Screenres size, size, 24
Cls
Line 1,057 ⟶ 1,604:
Next x
Bsave "twoDiagonalMatrix.bmp",0
Sleep</
{{out}}
https://www.dropbox.com/s/ph9r28gpkp8ao8n/twoDiagonalMatrix.bmp?dl=0
=={{header|Fortran}}==
Line 1,066 ⟶ 1,612:
{{works with|Fortran 95}}
Fortran stores data in columns. Therefore, it should be checked whether instead of filling the matrix row by row (as below) it would be better to do it column by column. The profit would be cache hit optimization, better communication between CPU and RAM. Fortran allows you to zero the entire array, such as the a array below, simply by substituting a = 0. Unfortunately, an array 100x100 (that is, the choosen maximum size) would be filled with zeros even if, as in the example, we would need a much smaller array. You can also eliminate the variables j1 and j2 by replacing them with i and n - i + 1 respectively, but the source code would be slightly less readable.
<
dimension a(100, 100)
Line 1,089 ⟶ 1,635:
end
</syntaxhighlight>
{{Output}}
<pre>
Line 1,103 ⟶ 1,649:
{{Works with|Fortran 77}}
Fortran is the oldest high-level programming language. It is constantly evolving and unfortunately there are no longer (or at least I do not have) computers on which to test whether the program works in the old Fortran IV or an even older dialect. The example below should be in Fortran IV, but unfortunately it was tested with a modern Fortran 2018 compiler, so even in legacy mode it is not Fortran IV but Fortran 77. However, it seems that it should work on CDC6000 mainframe ... if someone obviously has CDC6000.
<
PROGRAM PROG
Line 1,113 ⟶ 1,659:
A = 0.
DO 10 I = 1, N
A(I, I) = 1.
10 A(I, N - I + 1) = 1.
Line 1,119 ⟶ 1,665:
20 PRINT *, (A(I, J), J=1,N)
END</
=={{header|Go}}==
{{trans|Wren}}
<
import "fmt"
Line 1,144 ⟶ 1,690:
fmt.Println()
specialMatrix(9) // odd n
}</
{{out}}
Line 1,169 ⟶ 1,715:
=={{header|Haskell}}==
<
twoDiagonalMatrix :: Int -> [[Int]]
Line 1,186 ⟶ 1,732:
unlines . fmap (((' ' :) . show) =<<)
. twoDiagonalMatrix
<$> [7, 8]</
Or, in the form of a list comprehension:
<
twoDiagonalMatrix :: Int -> [[Int]]
Line 1,207 ⟶ 1,753:
unlines . fmap (((' ' :) . show) =<<)
. twoDiagonalMatrix
<$> [7, 8]</
{{Out}}
<pre> 1 0 0 0 0 0 1
Line 1,228 ⟶ 1,774:
and in terms of the Data.Matrix library:
<
twoDiagonals :: Int -> Matrix Int
Line 1,239 ⟶ 1,785:
main :: IO ()
main =
mapM_ print $ twoDiagonals <$> [7, 8]</
{{Out}}
<pre>┌ ┐
Line 1,262 ⟶ 1,808:
=={{header|J}}==
Implementation:
<
In other words, generate an order n identity matrix, flip it and (treating it as a bit matrix) OR the two matrices.
Line 1,271 ⟶ 1,816:
Some examples:
<
1 1
1 1
Line 1,295 ⟶ 1,840:
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1</
=={{header|Java}}==
The "Java philosophy" is the object-oriented paradigm. The example solution given below is therefore somewhat incomplete. We should first declare the matrix as an interface (or abstract class), then create a whole hierarchy of subclasses where DiagonalDiagonalMatrix would be a subclass of SquareMatrix or something like that. Of course, it's a gigantic job, but as a result we would have a library competing with Matlab / Octave etc., compliant with SOLID principles.
<syntaxhighlight lang="java">package example.diagdiag;
public class Program {
Line 1,353 ⟶ 1,899:
}
}</
{{Output}}
<pre> 1.0 0.0 0.0 0.0 0.0 0.0 1.0
Line 1,364 ⟶ 1,910:
=={{header|JavaScript}}==
<
"use strict";
Line 1,412 ⟶ 1,958:
// MAIN --
return main();
})();</
Or, in terms of a more general matrix function:
<
"use strict";
Line 1,472 ⟶ 2,018:
// MAIN ---
return main();
})();</
{{Out}}
<pre>1 0 0 0 0 0 1
Line 1,494 ⟶ 2,040:
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<
. as $n
| [range(0; $n) | 0] as $z
Line 1,501 ⟶ 2,047:
def display:
map(join(" ")) | join("\n");</
'''Example'''
<syntaxhighlight lang
{{out}}
<pre>
Line 1,518 ⟶ 2,064:
=={{header|Julia}}==
<
julia> twodiagonalmat(n) = [Int(i == j || i == n - j + 1) for j in 1:n, i in 1:n]
twodiagonalmat (generic function with 1 method)
Line 1,551 ⟶ 2,097:
0 1 0 1 0
1 0 0 0 1
</syntaxhighlight>
=== Sparse matrix version ===
<syntaxhighlight lang="julia">julia> using LinearAlgebra
julia> twodiagonalsparse(n) = I(n) .| rotl90(I(n))
twodiagonalsparse (generic function with 1 method)
julia> twodiagonalsparse(7)
7×7 SparseArrays.SparseMatrixCSC{Bool, Int64} with 13 stored entries:
1 ⋅ ⋅ ⋅ ⋅ ⋅ 1
⋅ 1 ⋅ ⋅ ⋅ 1 ⋅
⋅ ⋅ 1 ⋅ 1 ⋅ ⋅
⋅ ⋅ ⋅ 1 ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅ 1 ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅ 1 ⋅
1 ⋅ ⋅ ⋅ ⋅ ⋅ 1
julia> twodiagonalsparse(8)
8×8 SparseArrays.SparseMatrixCSC{Bool, Int64} with 16 stored entries:
1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1
⋅ 1 ⋅ ⋅ ⋅ ⋅ 1 ⋅
⋅ ⋅ 1 ⋅ ⋅ 1 ⋅ ⋅
⋅ ⋅ ⋅ 1 1 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 1 1 ⋅ ⋅ ⋅
⋅ ⋅ 1 ⋅ ⋅ 1 ⋅ ⋅
⋅ 1 ⋅ ⋅ ⋅ ⋅ 1 ⋅
1 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1
</syntaxhighlight>
=={{header|K}}==
K6
<syntaxhighlight lang="k">diag2: {x||x}@=:
diag2 5</syntaxhighlight>
{{out}}
<pre>(1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1)</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="Mathematica">
ClearAll[CreateMatrixWithTwoDiagonals];
CreateMatrixWithTwoDiagonals[n_Integer] :=
IdentityMatrix[n] + Reverse[IdentityMatrix[n]] -
If[OddQ[n], SparseArray[{{(n + 1)/2, (n + 1)/2} -> 1}, {n, n}], 0];
CreateMatrixWithTwoDiagonals[7] // MatrixForm
</syntaxhighlight>
{{out}}
<math>
\left(
\begin{array}{ccccccc}
1 & 0 & 0 & 0 & 0 & 0 & 1 \\
0 & 1 & 0 & 0 & 0 & 1 & 0 \\
0 & 0 & 1 & 0 & 1 & 0 & 0 \\
0 & 0 & 0 & 1 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 1 & 0 & 0 \\
0 & 1 & 0 & 0 & 0 & 1 & 0 \\
1 & 0 & 0 & 0 & 0 & 0 & 1 \\
\end{array}
\right)
</math>
=={{header|MATLAB}}==
<syntaxhighlight lang="matlab">function A = diagdiag(N, sparse)
% Create an diagonal-diagonal square matrix.
%
Line 1,581 ⟶ 2,191:
A = fliplr(A);
A(1:N+1:end) = 1;
end</
{{
<pre>
>> diagdiag(7)
Line 1,615 ⟶ 2,225:
(1,7) 1
(7,7) 1
</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a square matrix with a diagonal and antidiagonal pattern in their entries */
diags(n):=genmatrix(lambda([x,y],if x=y or x+y=n+1 then 1 else 0),n,n)$
/* Example */
diags(6);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 0],
[0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0],
[0, 1, 0, 0, 1, 0],
[1, 0, 0, 0, 0, 1]
)
</pre>
=={{header|Nim}}==
<syntaxhighlight lang="Nim">proc drawMatrix(side: Positive) =
let last = side - 1
for i in 0..<side:
for j in 0..<side:
stdout.write if i == j or i == last - j: "1 " else: "0 "
echo()
drawMatrix(6)
</syntaxhighlight>
{{out}}
<pre>1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
</pre>
=={{header|Pascal}}==
<
const N = 7;
type
Line 1,643 ⟶ 2,293:
end
end.
</syntaxhighlight>
{{Output}}
<pre> 1 0 0 0 0 0 1
Line 1,656 ⟶ 2,306:
=={{header|Perl}}==
===Strings===
<
use strict; #https://rosettacode.org/wiki/Matrix_with_two_diagonals
Line 1,669 ⟶ 2,319:
1 while s/(?<=1...{$n})0/1/s or s/(?<=2.{$n})[01]/2/s;
return tr/2/1/r =~ s/\B/ /gr;
}</
{{out}}
<pre>1 0 0 0 0 0 0 0 0 1
Line 1,695 ⟶ 2,345:
===Numbers===
<
use warnings;
use feature 'say';
Line 1,711 ⟶ 2,361:
say join ' ', @$_ for dual_diagonal(4); say '';
say join ' ', @$_ for dual_diagonal(5);
</syntaxhighlight>
{{out}}
<pre>1 0 0 1
Line 1,727 ⟶ 2,377:
===numbers===
Tee hee, pick the bones out of this one. ''Lovely.''
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">reinstate</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_mul</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}}),{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}}}),{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,750 ⟶ 2,400:
</pre>
Slightly saner, shows sackly same stuff:
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
Line 1,760 ⟶ 2,410:
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,{</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
===strings===
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">6</span> <span style="color: #008080;">to</span> <span style="color: #000000;">7</span> <span style="color: #008080;">do</span>
Line 1,773 ⟶ 2,423:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,796 ⟶ 2,446:
Based on [[2048#Phix]], and uses the same colour and font choices.
You can run this online [http://phix.x10.mx/p2js/strange_identity_matrices.htm here].
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">pGUI</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 1,808 ⟶ 2,458:
Press 'X' for two diagonals
The window can be fully resized
"""</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">help</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">IupMessage</span><span style="color: #0000FF;">(</span><span style="color: #000000;">TITLE</span><span style="color: #0000FF;">,</span><span style="color: #000000;">HELPTXT</span><span style="color: #0000FF;">,</span><span style="color: #000000;">bWrap</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 1,903 ⟶ 2,552:
<span style="color: #008080;">function</span> <span style="color: #000000;">key_cb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">Ihandle</span> <span style="color: #000080;font-style:italic;">/*ih*/</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_ESC</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_CLOSE</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (standard practice for me)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F5</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">IUP_DEFAULT</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span> <span style="color: #000080;font-style:italic;">-- (let browser reload work)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #004600;">K_F1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #000000;">help</span><span style="color: #0000FF;">()</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">=</span><span style="color: #008000;">'+'</span> <span style="color: #008080;">then</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
Line 1,910 ⟶ 2,560:
<span style="color: #008080;">if</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MOX"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">mtype</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">IupUpdate</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">IupOpen</span><span style="color: #0000FF;">()</span>
<span style="color: #000000;">canvas</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupCanvas</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"RASTERSIZE=532x532"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">
<span style="color: #000000;">dlg</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">IupDialog</span><span style="color: #0000FF;">(</span><span style="color: #000000;">canvas</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"MINSIZE=440x450"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">IupSetCallback</span><span style="color: #0000FF;">(</span><span style="color: #000000;">dlg</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"K_ANY"</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">Icallback</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"key_cb"</span><span style="color: #0000FF;">))</span>
Line 1,926 ⟶ 2,576:
<span style="color: #7060A8;">IupClose</span><span style="color: #0000FF;">()</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</
=={{header|PHP}}==
<
$n = 9; // the number of rows
Line 1,939 ⟶ 2,589:
echo "\n";
}
</syntaxhighlight>
{{Output}}
<pre> 1 0 0 0 0 0 0 0 1
Line 1,951 ⟶ 2,601:
1 0 0 0 0 0 0 0 1
</pre>
=={{header|PL/M}}==
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<syntaxhighlight lang="plm">
100H: /* DRAW SOME MATRICES WITH 1S ON THE DIAGONALS and 0S ELSEWHERE */
/* CP/M SYSTEM CALL AND I/O ROUTINES */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$NL: PROCEDURE; CALL PR$CHAR( 0DH ); CALL PR$CHAR( 0AH ); END;
/* TASK */
DRAW$DIAGONALS: PROCEDURE( N );
DECLARE N BYTE;
DECLARE ( I, J, R ) BYTE;
R = N;
DO I = 1 TO N;
DO J = 1 TO N;
CALL PR$CHAR( ' ' );
IF J = I OR J = R THEN CALL PR$CHAR( '1' );
ELSE CALL PR$CHAR( '0' );
END;
CALL PR$NL;
R = R - 1;
END;
END DRAW$DIAGONALS ;
CALL DRAW$DIAGONALS( 10 );
CALL PR$NL;
CALL DRAW$DIAGONALS( 11 );
EOF
</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 1 0 0
0 0 0 1 0 0 1 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 1 0 0 1 0 0 0
0 0 1 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 0 0 1 0
0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 0 1 0 0 0 0
0 0 0 1 0 0 0 1 0 0 0
0 0 1 0 0 0 0 0 1 0 0
0 1 0 0 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 0 0 1
</pre>
=={{header|Processing}}==
<syntaxhighlight lang="java">
//Aamrun, 27th June 2022
size(1000,1000);
textSize(50);
for(int i=0;i<10;i++){
for(int j=0;j<10;j++){
noFill();
square(i*100,j*100,100);
fill(#000000);
if(i==j||i+j==9){
text("1",i*100+50,j*100+50);
}
else{
text("0",i*100+50,j*100+50);
}
}
}
</syntaxhighlight>
=={{header|Python}}==
===Pure Python===
<
Line 2,005 ⟶ 2,737:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>1 0 0 0 0 0 1
Line 2,026 ⟶ 2,758:
===NumPy===
{{libheader|numpy}}
<
def diagdiag(n):
Line 2,037 ⟶ 2,769:
Returns:
a (numpy matrix): double diagonal matrix.
"""
d = np.eye(n)
Line 2,046 ⟶ 2,777:
return a
print(diagdiag(7))</
{{Output}}
<pre>
Line 2,056 ⟶ 2,787:
[0. 1. 0. 0. 0. 1. 0.]
[1. 0. 0. 0. 0. 0. 1.]]
</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery"> [ [] swap dup times
[ 0 over of
1 swap i poke
1 swap i^ poke
nested rot join swap ]
drop ] is two-diagonals ( n --> [ )
8 two-diagonals
witheach
[ witheach [ echo sp ] cr ]
cr
9 two-diagonals
witheach
[ witheach [ echo sp ] cr ]
</syntaxhighlight>
{{out}}
<pre>1 0 0 0 0 0 0 1
0 1 0 0 0 0 1 0
0 0 1 0 0 1 0 0
0 0 0 1 1 0 0 0
0 0 0 1 1 0 0 0
0 0 1 0 0 1 0 0
0 1 0 0 0 0 1 0
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 0 1
0 1 0 0 0 0 0 1 0
0 0 1 0 0 0 1 0 0
0 0 0 1 0 1 0 0 0
0 0 0 0 1 0 0 0 0
0 0 0 1 0 1 0 0 0
0 0 1 0 0 0 1 0 0
0 1 0 0 0 0 0 1 0
1 0 0 0 0 0 0 0 1
</pre>
=={{header|Raku}}==
<syntaxhighlight lang="raku"
.say for dual-diagonal(6);
say '';
.say for dual-diagonal(7);</
{{out}}
<pre>[1 0 0 0 0 1]
Line 2,081 ⟶ 2,851:
=={{header|Red}}==
<
x-matrix: function [size][
Line 2,095 ⟶ 2,865:
x-matrix 6
prin newline
x-matrix 7</
{{out}}
<pre>
Line 2,115 ⟶ 2,885:
=={{header|Ring}}==
<
# Project : Identity Matrix
# Date : 2022/16/02
Line 2,182 ⟶ 2,952:
next
score = 0
</syntaxhighlight>
Outpu image:
<br>
[http://keptarhely.eu/view.php?file=20220217v00xdle18.jpeg Special identity matrix with two diagonals]
=={{header|RPL}}==
The approach here is to fill the second diagonal of an identity matrix generated by the <code>IDN</code> instruction, thanks to a classical <code>FOR.. NEXT</code> loop.
{{works with|Halcyon Calc|4.2.7}}
≪ IDN LAST
1 OVER '''FOR''' line
line OVER 2 →LIST ROT SWAP 1 PUT
SWAP 1 -
'''NEXT''' DROP
≫ ‘XDIAG’ STO
5 XDIAG
6 XDIAG
{{out}}
<pre>
2: [[ 1 0 0 0 1 ]
[ 0 1 0 1 0 ]
[ 0 0 1 0 0 ]
[ 0 1 0 1 0 ]
[ 1 0 0 0 1 ]]
1: [[ 1 0 0 0 0 1 ]
[ 0 1 0 0 1 0 ]
[ 0 0 1 1 0 0 ]
[ 0 0 1 1 0 0 ]
[ 0 1 0 0 1 0 ]
[ 1 0 0 0 0 1 ]]
</pre>
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">require 'matrix'
class Matrix
def self.two_diagonals(n)
Matrix.build(n, n) do |row, col|
row == col || row == n-col-1 ? 1 : 0
end
end
end
Matrix.two_diagonals(5).row_vectors.each{|row| puts row.to_a.join(" ") }</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
</pre>
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func dual_diagonal(n) {
n.of {|k|
var r = (k.of(0) + [1] + (n - k - 1).of(0))
r ~Z| r.reverse
}
}
dual_diagonal(5).each{.join(' ').say}; say ''
dual_diagonal(6).each{.join(' ').say}</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
</pre>
=={{header|Uiua}}==
<syntaxhighlight lang="Uiua">
DoubleMatrix ← ↥⇌.⊞=.⇡
DoubleMatrix 4
</syntaxhighlight>
{{out}}
<pre>
╭─
╷ 1 0 0 1
0 1 1 0
0 1 1 0
1 0 0 1
╯
</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn special_matrix(n int) {
for i in 0..n {
for j in 0..n {
if i == j || i+j == n-1 {
print("1 ")
} else {
print("0 ")
}
}
println('')
}
}
fn main() {
special_matrix(6) // even n
println('')
special_matrix(5) // odd n
}</syntaxhighlight>
{{out}}
<pre>
1 0 0 0 0 1
0 1 0 0 1 0
0 0 1 1 0 0
0 0 1 1 0 0
0 1 0 0 1 0
1 0 0 0 0 1
1 0 0 0 1
0 1 0 1 0
0 0 1 0 0
0 1 0 1 0
1 0 0 0 1</pre>
=={{header|Wren}}==
A terminal based solution as I don't like asking people to view external images.
<
for (i in 0...n) {
for (j in 0...n) {
Line 2,200 ⟶ 3,092:
specialMatrix.call(6) // even n
System.print()
specialMatrix.call(7) // odd n</
{{out}}
Line 2,221 ⟶ 3,113:
=={{header|XPL0}}==
<
int S, I, J;
[for I:= 0 to S-1 do
Line 2,231 ⟶ 3,123:
[DrawMat(6); CrLf(0);
DrawMat(7); CrLf(0);
]</
{{out}}
|