MatrixForm[mat = {
{0, 0, -(1/Sqrt[2]), 1/Sqrt[2]},
{0, 0, 1/Sqrt[2], 1/Sqrt[2]},
{0, 0, 0, 0},
{0, 0, 0, 0}}]
MapIndexed[(row[First[#2]] = Union[#1] == {0}) &, mat];
MapIndexed[(col[First[#2]] = Union[#1] == {0}) &, Transpose[mat]];
f[r_, c_] := If[row[r] && col[c], Sow[{r, c}]];
pos = Reap[Outer[f, Sequence @@ Range /@ Dimensions[mat]]][[-1, 1]];
MatrixForm[ReplacePart[mat, pos -> 1]]
{{0, 0, -(1/Sqrt[2]), Sqrt[2]},
{0, 0, 1/Sqrt[2], 1/Sqrt[2]},
{1, 1, 0, 0},
{1, 1, 0, 0}}
not quite the desired result
version 2
MatrixForm[mat = {
{0, 0, -(1/Sqrt[2]), 1/Sqrt[2]},
{0, 0, 1/Sqrt[2], 1/Sqrt[2]},
{0, 0, 0, 0},
{0, 0, 0, 0}}]
MapIndexed[(row[First[#2]] = Union[#1] == {0}) &, mat];
MapIndexed[(col[First[#2]] = Union[#1] == {0}) &, Transpose[mat]];
f[r_, c_] := If[row[r] && col[c],
mat = ReplacePart[mat, {r, c} -> 1];
row[r] = col[c] = False];
Outer[f, Sequence @@ Range /@ Dimensions[mat]];
MatrixForm[mat]
{{0, 0, -(1/Sqrt[2]), Sqrt[2]},
{0, 0, 1/Sqrt[2], 1/Sqrt[2]},
{1, 0, 0, 0},
{0, 1, 0, 0}}
as a function
place[$mat_] := Module[{row, col, f, mat = $mat},
MapIndexed[(row[First[#2]] = Union[#1] == {0}) &, mat];
MapIndexed[(col[First[#2]] = Union[#1] == {0}) &, Transpose[mat]];
f[r_, c_] := If[row[r] && col[c],
mat = ReplacePart[mat, {r, c} -> 1];
row[r] = col[c] = False];
Outer[f, Sequence @@ Range /@ Dimensions[mat]];
mat]
place[{
{0, 0, -(1/Sqrt[2]), 1/Sqrt[2]},
{0, 0, 1/Sqrt[2], 1/Sqrt[2]},
{0, 0, 0, 0},
{0, 0, 0, 0}}] // MatrixForm
{{0, 0, -(1/Sqrt[2]), Sqrt[2]},
{0, 0, 1/Sqrt[2], 1/Sqrt[2]},
{1, 0, 0, 0},
{0, 1, 0, 0}}
m1
orm2
?m = {{0, 0, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 2}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; {(m1 = {{0, 0, 0, 3}, {1, 0, 0, 0}, {0, 0, 0, 2}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}}) // MatrixForm , (m2 = {{0, 0, 0, 3}, {0, 0, 0, 0}, {0, 0, 0, 2}, {1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}}) // MatrixForm}
$\endgroup$