# How to efficiently fill in matrix values?

I would like to efficiently fill in or assign values to matrix m:

m={
{a[1,1],a[1,2],a[1,3],a[1,4]},
{a[2,1],a[2,2],a[2,3],a[2,4]},
{a[3,1],a[3,2],a[3,3],a[3,4]},
{a[4,1],a[4,2],a[4,3],a[4,4]}};


a[1,1] to a[4,1] to be equal to 1.

a[1,2] to a[4,2] to be equal to another column of value: a[1,3] to a[4,3] to be equal to another column of value: and

a[1,4] to a[4,4] to be evaluated based on the matrix locations of the other a[1,1] to a[4,3] cells as in excel cell format of:

a[1,4] = a[1,1]*a[2,3] + etc etc

but as soon as those dependencies are replaced with actual numbers, for example a[1,1] is now no longer symbolic but actual values, then how to symbolically do those evaluations based on cell locations similar to Excel as I will reuse this matrix again? Maybe use Table or something to evaluate and then join them together into a large matrix?

• Try for example m = Array[0 &, {4, 4}]; m[[1 ;; 4, 1]] = 1 – Dr. belisarius Dec 19 '12 at 19:54
• My interpretation of this question is that he mainly wants to know how "calculated columns" are realizable in Mathematica. So I tried to address that in a bare-bones way in my answer. If this isn't what's being asked, please make the question clearer. – Jens Dec 19 '12 at 20:03

Maybe it's worth giving a really simple answer so that it becomes clear that Mathematica isn't a spreadsheet but can do the same things even if you don't try very hard to make it look like a spreadsheet.

What you want is to have a matrix of values that you can change, and a set of results that depend on the entries of that matrix. So perhaps you could do the following. First I define a matrix a with some numerical coefficients that are chosen randomly:

a = RandomReal[1, {4, 3}];
MatrixForm[a]


$\left( \begin{array}{ccc} 0.683175 & 0.154214 & 0.244709 \\ 0.0457232 & 0.123932 & 0.823389 \\ 0.835944 & 0.228088 & 0.0983972 \\ 0.426466 & 0.540916 & 0.321899 \\ \end{array} \right)$

Now I define the results list. It could be a column of the same length as in a, but it doesn't have to be. So here I just choose two result entries:

results=Dynamic[{a[[1,1]]+a[[2,3]],3 a[[2,2]]-a[[4,3]]}]


{1.50656,0.049896}

The entries of results are calculated from the values in a, addressed by double square brackets (Part). The whole thing is wrapped in Dynamic so that the displayed entries are now going to be continuously updated if you change the entries in a. For example, try changing

a[[1, 1]] = 10;


and keep an eye on the results output on the previous line. It will change because the values are re-calculated.

The manner in which you change entries in a could now be made fancier in many ways, but this illustrates the idea.

If you're not after dynamically updated results, then you might as well define results as a function of the matrix,

Clear[results];
results[a_?ArrayQ] := {a[[1, 1]] + a[[2, 3]], 3 a[[2, 2]] - a[[4, 3]]}

results[a]


{1.50656,0.049896}

leading to the same result display, but now with the added flexibility that you can supply an arbitrary matrix to this function and have it access the elements of that matrix in the same way as defined before to form the result.