# parsed matrix not deleting elements in adjusted matrix

I have a matrix that has been parsed to delete certain rows and columns, as well as delete certain elements on the diagonal on the matrix. The problem is that it does this in theory when i take the elements in terms of $w[i,j]$. However, when I put in an equation for $w[i,j]$, it does not actually delete the elements. Here is the code in theory where it works

M = 10;
initselect = 5;
secselect = 6;
k[i_] := Sum[w[i, j], {j, 1, M}];
V[i_, j_] := Piecewise[{{w[i, i] - k[i], i == j}, {w[i, j], i > j}, {w[i, j], i < j}}]
matrix = Table[V[i, j], {i, M}, {j, M}];

(*This generates the original matrix that is unparsed*)

(*MatrixForm[matrix]*)

stay = Join[Range[initselect], Range[secselect, M, 2]];
TrueMatrix = matrix[[stay, stay]];

(*This generates the matrix that has parsed the selected rows and columns*)

MatrixForm[TrueMatrix]

(*delete=Complement[Range[M],Join[Range[5],Range[6,M,2]]];*)

delete = Complement[Range[M], stay];
TrueMatrix /. w[_, Alternatives @@ delete] -> 0 // MatrixForm


It initially deletes the appropriate rows and columns to get the TrueMatrix, then the last line it also deletes the appropriate elements on the diagonal.

Now here is the code with an equation for $w[i,j]$

M = 10;
initselect = 5;
secselect = 6;
k[i_] := Sum[w[i, j], {j, 1, M}];
w[i_, j_] := i*j^2
V[i_, j_] := Piecewise[{{w[i, i] - k[i], i == j}, {w[i, j], i > j}, {w[i, j], i < j}}]

matrix = Table[V[i, j], {i, M}, {j, M}];

(*This generates the original matrix that is unparsed*)

(*MatrixForm[matrix]*)

stay = Join[Range[initselect], Range[secselect, M, 2]];
TrueMatrix = matrix[[stay, stay]];

MatrixForm[TrueMatrix]
(*This generates the matrix that has parsed the selected rows and columns*)

(*MatrixForm[TrueMatrix];*)

(*delete=Complement[Range[M],Join[Range[5],Range[6,M,2]]];*)

delete = Complement[Range[M], stay];
TrueMatrix /. w[_, Alternatives @@ delete] -> 0 // MatrixForm


Here when I print the TrueMatrix and when it computes the last line to get the final matrix, they are identical which shouldn't be the case and isn't the case when I just use $w[i,j]$ without and expression.

• Hi ! This is the fourth question you post here. I highly doubt it you did not notice the code formatting. Please, head to the help centre and learn how to properly format your code. – Sektor Dec 21 '14 at 15:43
• ok, thanks. I just took the tutorial in the help center. – user2558894 Dec 21 '14 at 16:23
• I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Dr. belisarius Dec 21 '14 at 16:33
• 1) w is not defined in the first block of code. 2) In the second block delete is {7,9}. It looks like you want to map multiples of 49 and 81 to 0. However, it doesn't look like they are present in TrueMatrix. 3) w[_, Alternatives @@ delete] evaluates to (7 | 9)^2 _ not something that looks like anything in your matrix. – Sjoerd C. de Vries Dec 21 '14 at 22:58