Matrix with two diagonals: Difference between revisions

Content deleted Content added
Querfeld (talk | contribs)
mNo edit summary
m [Uiua] ⇌ works on the rows of the array by default, so ≡ isn't needed.
(6 intermediate revisions by 5 users not shown)
Line 1,023:
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>
 
=={{header|Excel}}==
Line 2,099 ⟶ 2,126:
</syntaxhighlight>
 
=={{header|MatlabK}}==
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 2,128 ⟶ 2,192:
A(1:N+1:end) = 1;
end</syntaxhighlight>
{{Outputout}}
<pre>
>> diagdiag(7)
Line 2,966 ⟶ 3,030:
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}}
Line 3,004 ⟶ 3,081:
=={{header|Wren}}==
A terminal based solution as I don't like asking people to view external images.
<syntaxhighlight lang="ecmascriptwren">var specialMatrix = Fn.new { |n|
for (i in 0...n) {
for (j in 0...n) {