# How to build my desire matrix using SparseArray?

I have built some lists as follows:

a[1] = {1.000000, 0.999994, 0.999407, 0.969414, 0.876121, 0.829982,
0.814620, 0.809723, 0.808173};
a[2] = {1.000000, 0.999996, 0.999587, 0.975533, 0.882113, 0.832107,
0.815302, 0.809940, 0.808241};
a[3] = {1.000000, 0.999997, 0.999665, 0.978707, 0.885764, 0.833423,
0.815726, 0.810074, 0.808284};
a[4] = {1.000000, 0.999997, 0.999735, 0.981924, 0.890005, 0.834976,
0.816227, 0.810234, 0.808335};
a[5] = {1.000000, 0.999998, 0.999819, 0.986419, 0.897291, 0.837710,
0.817110, 0.810515, 0.808423};
a[6] = {1.000000, 0.999998, 0.999844, 0.987933, 0.900267, 0.838853,
0.817480, 0.810632, 0.808460};
a[7] = {1.000000, 0.999998, 0.999850, 0.988260, 0.900959, 0.839085,
0.817555, 0.810655, 0.808469};
a[8] = {1.000000, 0.999998, 0.999850, 0.988260, 0.900959, 0.839121,
0.817567, 0.810659, 0.808469};
a[9] = {1.000000, 0.999998, 0.999850, 0.988286, 0.901014, 0.839143,
0.817574, 0.810661, 0.808469};
a[10] = {1.000000, 0.999998, 0.999850, 0.988296, 0.901037, 0.839152,
0.817577, 0.810662, 0.808470};

b[1] = {0.000000, 0.000003, 0.000298, 0.021117, 0.410215, 4.732543,
49.173238, 497.405074, 4991.813767};
b[2] = {0.000000, 0.000003, 0.000249, 0.018938, 0.400996, 4.706122,
49.093389, 497.156451, 4991.031476};
b[3] = {0.000000, 0.000002, 0.000224, 0.017692, 0.395239, 4.689644,
49.043735, 497.002015, 4990.545724};
b[4] = {0.000000, 0.000002, 0.000199, 0.016325, 0.388406, 4.670093,
48.984959, 496.819371, 4989.971417};
b[5] = {0.000000, 0.000002, 0.000165, 0.014179, 0.376287, 4.635388,
48.880985, 496.496718, 4988.957410};
b[6] = {0.000000, 0.000002, 0.000153, 0.013376, 0.371188, 4.620756,
48.837283, 496.361283, 4988.531880};
b[7] = {0.000000, 0.000002, 0.000150, 0.013195, 0.369991, 4.617781,
48.828408, 496.333788, 4988.432061};
b[8] = {0.000000, 0.000002, 0.000150, 0.013195, 0.369991, 4.617317,
48.827026, 496.329506, 4988.432061};
b[9] = {0.000000, 0.000002, 0.000150, 0.013180, 0.369894, 4.617041,
48.826200, 496.326955, 4988.424026};
b[10] = {0.000000, 0.000002, 0.000150, 0.013175, 0.369855, 4.616927,
48.825863, 496.325903, 4988.420744};
logo={-4, -3, -2, -1, 0, 1, 2, 3, 4};
mu={0.5, 0.6, 0.666666666667, 0.75, 0.909090909091, 0.980392156863, \
0.995192307692, 0.997506234414, 0.998890122087, 0.999455634186};


where each a[i] and b[i] belongs to a special mu[[i]] and each a[i][[j]] and b[i][[j]] belongs to a special logo[[j]]. For example a[1] corresponds to mu[[1]] and a[1][[2]] corresponds to logo[[2]]. Now I want to collect all this information in a single matrix which has dimension (10,9,4) where 10 is number of a's and b's and length of mu, 9 is length of each a[i] and b[i] and also logo. and 4 represents inner corresponding members of each element of matrix as follows: (a,b,mu,logo) -In fact I want to collect each a and b corresponds to the same mu's and same logo's in a four-member list. I hope I have conveyed what I mean!

so I tried this command:

datamat =
Table[SparseArray[{{i, j, 1} -> a[i][[j]], {i, j, 2} ->
b[i][[j]], {i, j, 3} -> \[Mu][[i]], {i, j, 4} -> logo[[j]]}], {i,
1, 10}, {j, 1, 9}]


this works partially but there is a problem which produces a lot of zero elements, how can deal with this problem? and is there any alternative to build such a matrix? Thanks in advance.

• I read your post twice, still not clear. Maybe try to use mathematical notations? Where do you get zeros that should not be there? Sep 12 at 7:00
• Trying Normal[datamat]//MatrixaForm you can see the zeros Sep 12 at 7:10

This definition doesn't use SparseArray, but it should give it to you

datamat = Table[{a[i][[j]], b[i][[j]], mu[[i]], logo[[j]]},
{i, 10}, {j, 9}];


For example, with $$i=5$$ and $$j=9$$, select all 4 corresponding values by evaluating datamat[[5,9]] (* {0.808423, 4988.96, 0.909091, 4} *). These four values are the 9th element of a[5], the 9th element of b[5], mu[[5]] and logo[[9]].

• Haha! You're right! I don't know why such a simple solution did not cross my mind!! Anyway Thanks a lot. Sep 12 at 8:31

Probably not easier to understand than Table, but index- and dimension-free:

Join[
Map[List, Values@DownValues@a, {2}],
Map[List, Values@DownValues@b, {2}],
Outer[List, mu, logo],
3]


Variations are possible, such as this nifty one which uses the dimension 10:

Values@DownValues@a === Array[a, 10]
(*  True  *)


One could then map List inside Array instead of outside Values: Array[Map[List]@*a, 10]. Or this variant to construct the whole array:

Transpose[
{Array[a, 10],  (* arrange the four values in *)
Array[b, 10],  (* individual arrays          *)
Transpose[Table[mu, Length@logo]],
Table[logo, Length@mu]},
{3, 1, 2}]      (* and transpose to 4-tuples  *)