# How to evaluate the matrix in loop for different values of entries?

Hi everybody: I have a 4x4 matrix M see below, where for each value of h I have corresponding a,b,c,d values (please see the link below). I need to evaluate the matrix M for each value of h and corresponding a,b,c,d to determine the eigenvalue and eigenvectors and save them in .txt file. So far, I have tried following way, where I can compute the said matrix by specifying the values separately but I couldn't set up a loop. I would really appreciate any help.

In this file, for each value of h we have a, b, c and d values.



Mode1 = Import["CF.txt", "Table"];
M = {{h, 0, a, c}, {0, h, d, b}, {a, c, h/2, 0}, {b, d, 0, h/2}};
{eigs, vecs} = Eigensystem[M];
list1 = Partition[Riffle[eigs, Chop[vecs]], 2];
list2 = Sort[list1, #1[] < #2[] &];
list2 // MatrixForm


• Try this f[v_]:=Sort[v,N[#1[]]<N[#2[]]&]; Mode1=Import["CF.txt","Table"]; Map[({h,a,b,c,d}=#; M={{h,0,a,c},{0,h,d,b},{a,c,h/2,0},{b,d,0,h/2}}; {eigs,vecs}= Eigensystem[M]; list1=Partition[Riffle[eigs,Chop[vecs]],2]; list2=f[list1]; list2 // MatrixForm)&, Mode1] and verify it is doing the assignment correctly for you
– Bill
May 3 '20 at 4:09
• Hi @Bill , first of all thank you very much I verified the results and its working. But I am trying to export the results using Export["CF.dat", list2] within a Map, it will generate multiple files equivalent to the values of h, when I introduce outside of Map function it will generate only last eigenvalues and eigenvectors. Is it possible, that we can write all the values corresponding to each h in one file.
– Aman
May 3 '20 at 14:00

If you want to export all the results from Map then this seems very simple to do:

f[v_]:=Sort[v,N[#1[]]<N[#2[]]&];
Mode1=Import["CF.txt","Table"];
listall=Map[(
{h,a,b,c,d}=#;
M={{h,0,a,c},{0,h,d,b},{a,c,h/2,0},{b,d,0,h/2}};
{eigs,vecs}= Eigensystem[M];
list1=Partition[Riffle[eigs,Chop[vecs]],2];
f[list1])&, Mode1
];
Export["CF.dat", listall]

• Thank you very much @Bill. Its working.
– Aman
May 3 '20 at 17:39