# Trying to get the matrix except the diagonal elements

Posted 10 years ago
6541 Views
|
10 Replies
|
11 Total Likes
|
 Hi. I am trying to get the matrix without the diagonal elements with the following code:cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];cvv = MatrixForm[Simplify[cv, Element[u[_, _], Reals]]];MatrixForm[Simplify[cvv, Except[Diagonal[cvv]]]]However, I cannot get it and receive the following error message:Diagonal::list: List expected at position 1 in Diagonal[(1/3 (6 <<2>>+<<2>> u[<<2>>]-4 u[<<2>>]^2)    -(1/3) <<3>> u[<<2>>]-(1/3) <<3>> u[<<2>>]    1/3 (<<1>>))]. >>I would greatly appreciate if you could help me with this inquiry..Thank you very much!
10 Replies
Sort By:
Posted 10 years ago
 Thank you so much again!If I really need to find the abbreviated formula for the given numeric result,What other computer program can do this job other than Mathematica..?Could you pleae recommend any of them?I really do appreciate your help!
Posted 10 years ago
 Mathematica is very determined to do things the way it wants to do them.For sums of a finite number of terms Mathematica always believes showing all the terms is what it wants to do instead of collapsing the terms back into a sum, even if that sum might be more compact.There were some research papers published in the 1980s on trying to find the most compact form of an expression. I was struggling with complicated expressions at that time and trying to find far simpler forms, but I have not thought about that in many years and I could not tell you where to begin looking for those papers or whether that was ever implemented in software you could find and use.I suspect that Mathematica will resist with all the resources it has any attempt by you to coax it to put expressions into a compact sum notation.Simplify accepts an option ComplexityFunction which you can look up in the help system. By defining the value of ComplexityFunction you can try to direct Simplify to understand what you think is a simpler form. In my experience this is not a task for the inexperienced user. I have never had success with this for any complicated task.
Posted 10 years ago
 cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];m = Simplify[cv, Element[u[_, _], Reals]];Total[m - IdentityMatrix*Diagonal[m]];Simplify[%]You can't just type Diagonal. It needs an argument. Also, there is no need to Simplify the expression before calling Total. Not needed. You can always simplify the final result when it is done. (after Total is called)Also, you can just use ComplexExpand instead of the second line above. Easier. Like this:cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];m = ComplexExpand[cv];Total[Flatten[m - IdentityMatrix*Diagonal[m]]];ComplexExpands assumes all variables are real. So it does the job for you allready that you were trying to do using your Simplify command.
Posted 10 years ago
 In:= cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];m = Simplify[cv, Element[u[_, _], Reals]];Total[Flatten[Simplify[m - IdentityMatrix*Diagonal[m]]]]Out= 1/3 (u[1, 3] u[2, 0] - 3 u[1, 2] u[2, 1] + 6 u[1, 2] u[2, 2] - 3 u[1, 3] u[2, 2] - 3 u[1, 1](u[2, 0] -  2 u[2, 1] + u[2, 2]) - 3 u[1, 2] u[2, 3] + 2 u[1, 3] u[2, 3] + u[1, 0] (2 u[2, 0] - 3 u[2, 1] + u[2, 3]))Trying a very small example, perhaps 3x3 like I did, to verify if that is working can help you track down problems you can't see in larger problems.Being extremely careful to not make small mistakes, like leaving the [ m ] off the end of Diagonal as you did, is very important in Mathematica.Mathematica is not like a bright graduate student who can see all your typographical errors and understand what you meant and give you thecorrect answer anyway. There is no "do what I mean" button which can do that in Mathematica yet and I do not expect one any time soon.REALLY understanding the "Mathematica way of thinking" and mastering all the details of the first 200 Mathematica functions that you will useevery day will be of great assistance to you in the future and make your life much easier.
Posted 10 years ago
 Thank you Bill! You are quite a huge helper!Hope you have successes for your future careers as well! Thanks to Nasser as well! To be honest, my final step towards the problem will be having the simplest expression of theresult that I can use in practice. For this reason, something like including the expression likesigma (for summation not for standard deviation) or the most convenient way to understand the result will be quite useful for making the extenuating practical test for this result.The final result that I want to make into the simplest expression to be used in practice and extended useageswill be generated from the following codes:as = Variance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];aa = Total[Flatten[FullSimplify[as, Element[u[_, _], Reals]]]];cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];m = FullSimplify[cv, Element[u[_, _], Reals]];bb = Total[Flatten[FullSimplify[m - IdentityMatrix*Diagonal]]];D[Simplify[((aa + 2*bb)^(1/2))*F, Element[u[_, _], Reals]], u[1, 2]]-> here, the u[1,2] in the last part of the last line can vary. This is the reason why I am trying to derivea generalized expression for future usages.Is there a way to do so?Again..Thank you very much for you sincere helps and hope all successes for your future endeavors!
Posted 10 years ago
 Does this example help solve part of your problem?In:= m = {{a, b, c}, {d, e, f}, {g, h, i}};m - IdentityMatrix*Diagonal[m]Out= {{0, b, c}, {d, 0, f}, {g, h, 0}}
Posted 10 years ago
 Thanks, Bill! That help was tremendous!Actually, I do have one more step that I would like to ask you about..I am trying to sum all the elements of the final resulting matrix from the following code:cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];m = Simplify[cv, Element[u[_, _], Reals]];Simplify[m - IdentityMatrix*Diagonal]Then I tried the following command:Total[Simplify[m - IdentityMatrix*Diagonal]]but did not sum all the elements..Is there a way to do so?Again, thank you so much!
Posted 10 years ago
 Thank you!If I am trying to use without the "Matrix" format by using the following code:cv = Covariance[Table[u[i, t] - u[i, t - 1], {t, 1, 3}, {i, 1, 2}]];Diagonal[Simplify[cv, Element[u[_, _], Reals]]];Cases[Simplify[cv, Element[u[_, _], Reals]],Except[Diagonal[Simplify[cv, Element[u[_, _], Reals]]]]]-> Even this does not seem to give the desired result since the list before deleting the diagonal and after deleting the diagonal seemed the same..So..my question is:(1) Am I doing the right code for my purpose? If not, what should be the code for this?(2) For the covariance matrix produced above, without the Element[u[_, _], Reals] command, it seems like it is producing some conjugates that are not my desired result. And also, I am not 100% confident that even though including the command Element[u[_, _], Reals] eleminates the conjugates, I wonder whether I am getting the correct result for my further implementation since I think it can also change the nature of the components as well.I would greatly appreciate if I could solve these two crucial problems.Thank you very much!
Posted 10 years ago
 as Bill pointed out, MatrixForm changes the head of the expression. Two common ways to show the matrix as MatrixForm and still be able to use the matrix as List, is to protect the matrix, like this: Another way, but not recommended is to do this:mat = MatrixForm@ RandomReal[1, {3, 3}]Head[mat]Det[First@mat]What this does is keep the MatrixForm head in there, attached to the data but uses First@mat to jump into the data inside it. But again, this is not a good way to handle it. Better to use one of the above two methods.
Posted 10 years ago
 MatrixForm will show a pretty example to look at, but that cannot be used for any further calculations.