Tag Info

New answers tagged

4

None of the above are particularly "efficient", if that's your goal. By way of example, mat = Module[{p1 = Range[0, 2 # - 2, 2], p2}, p2 = p1*2; p1[[1 ;; ;; 2]] = 0; p2[[2 ;; ;; 2]] = 0; LowerTriangularize@Transpose[PadRight[{p1}, #, {2 p1, p2}]]] &; mat[200];//Timing (* {0., Null} *) And that's on an old netbook. Compared to ...


4

Here is my version using SparseArray: mat2[n_]:=SparseArray[{{i_,j_}/;And[i>j,Mod[i+j,2]==1]:> If[j==1,2,4](i-1)},{n,n},0.]; MatrixForm@mat2[10]


8

Update: here Table is faster and more user-friendly then Array. mat[n_] := LowerTriangularize@Table[2 (1 + Boole[j > 1]) (i - 1) Mod[i + j, 2], {i, n}, {j, n}]; mat[10] // MatrixForm It is fast and the result is packed array mat[1000] // Developer`PackedArrayQ // AbsoluteTiming (* {0.142522, True} *)


1

I think this meets the spec: gKirkland[rows_, columns_] := Outer[ Function[{i, j}, If[i <= j, 0, If[Mod[i + j, 2] == 0, 0, 1] If[j == 1, 1/2, 1] (4 (i - 1)) ] ], Range[1, rows], Range[1, columns]] gKirkland[16,16] gives the matrix as shown. gKirkland[1000,1000] evaluates in 3 seconds.



Top 50 recent answers are included