Tag Info

New answers tagged

1

Updating an existing matrix randomP = RandomInteger[{0, 9}, {5, 5}]; m = 2 ReplacePart[randomP, {{i_, j_} /; And[j == 2 i, j == m] -> p, {i_, j_} /; Or[j == 2 i - m, j == 0] -> q}]


4

m = 5; sa = SparseArray[{{i_, j_} /; j == Min[2 i, m] :> p, {i_, j_} /; j == 2 i - m :> q}, {m, m}] mat = Partition[Range[m^2], m]; sa2 = SparseArray[{{i_, j_} /; j == Min[2 i, m] :> p, {i_, j_} /; j == 2 i - m, 0] :> q, {i_, j_} :> mat[[i, j]]}, {m, m}]; Row[MatrixForm /@ {mat, sa2}]


1

There's no need to update values; you can use Table and create it in one pass. m = 4; Table[ Which[ j == 2 i && j == m, p, j == 2 i - m || j == 0, q, True, 0 ], {i, 10}, {j, 10}] // MatrixForm



Top 50 recent answers are included