1
$\begingroup$

I have this piece of code which populates a matrix following different rules depending on row and column number:

MQp := Array[Qp, {Length[a], Length[a]}];
Do[Qp[i, 1] = a[[i]], {i, 1, Length[a]}];
Do[Qp[1, j] = a[[j]]/2, {j, 2, Length[a]}];
Do[If[(i == j), 
   If[((2 j - 1) <= Length[a]), Qp[i, j] = a[[2 i - 1]]/2 + a[[1]], 
    Qp[i, j] = a[[1]]], 
   If[(i != 1 && j != 1), 
    If[(i + j - 1) <= Length[a], 
     Qp[i, j] = a[[i + j - 1]]/2 + a[[Abs[i - j] + 1]]/2, 
     Qp[i, j] = a[[Abs[i - j] + 1]]/2]]], {i, 1, Length[a]}, {j, 1, 
   Length[a]}];
Qp[1, 1] = a[[1]];
MatrixForm[MQp]

Currently "a" is a table of numerical values. I would like to generate a table of symbolic a's, and then be able to substitute the numerical values into the matrix. This way I can easily check that the values in the matrix correctly match what should be generated. I've tried several methods, but so far I get errors or nothing is output at all? Can someone help me out? Thanks in advance.

PS: How do I display code blocks correctly? I did a line and eight spaces but it looks like it didn't work.

$\endgroup$
  • $\begingroup$ You can display the code block by highlighting the code and pressing the button labeled {} in the editor. I just fixed it above. $\endgroup$ – bill s Feb 2 '14 at 21:44
3
$\begingroup$

You can generate an array of symbolic values using Array

Clear[a];
a = Array[A, 5];

Then your matrix MQp becomes:

enter image description here

You can then check for specific values using a pattern. For example,

MQp /. {A[2] -> 3}

gives the matrix with 3 in place of A[2].

$\endgroup$
1
$\begingroup$

Here's is another way to define your matrix using SparseArray[]. Perhaps it's easier to understand:

a = Array[A, 5];
b = SparseArray[{
   {i_, 1} :> a[[i]],
   {1, j_} :> a[[j]]/2,
   {i_, i_} /; (2 i - 1) <= Length@a :> a[[2 i - 1]]/2 + a[[1]],
   {i_, i_} /; (2 i - 1) >  Length@a :> a[[1]],
   {i_, j_} /; (i + j - 1) <= Length[a] :> a[[i + j - 1]]/2 + a[[Abs[i - j] + 1]]/2,
   {i_, j_} /; (i + j - 1) >  Length[a] :> a[[Abs[i - j] + 1]]/2},
  Length@a {1, 1}]
b // MatrixForm  

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.