The code
generate[matrix_] := Module[{n}, n := Dimensions[matrix][[1]];
Normal@Simplify@SparseArray[{
{i_, j_} /; i == 1 -> 2
}, {n, n}]]
a := {{1, 2}, {3, 4}}
generate[a]
produces
{{2, 2}, {0, 0}}
(of course there is no need to define the matrix a in the first place in this case). Now consider the modification
generate[matrix_] := Module[{n}, n := Dimensions[matrix][[1]];
Normal@Simplify@SparseArray[{
{i_, j_} /; i == 1 -> matrix[[i,j]]
}, {n, n}]]
a := {{1, 2}, {3, 4}}
generate[a]
where I only changed 2 into matrix[[i,j]]. Just like the first example generates a 2x2 matrix with zeros everywhere and fills in the first row with 2's, I would expect the second snippet of code to generate a matrix whose first row is the same as that of the input matrix a. Instead, I get the error message
Part: The expression i$ cannot be used as a part specification.
How would I go about solving this? Of course in this example I could just set each entry manually, but this example is just a proxy of the more complicated case I'm working with. I'm new to Mathematica. Thanks!
:>(RuleDelayed) instead of->(Rule) to prevent evaluation of thematrix[[i,j]]part beforeiandjare substituted. $\endgroup$