8
$\begingroup$

I have a matrix generated like this :

Table[Sequence @@ {f[t[[i]], j, x], g[t[[i]], j, x]}, {i, 16}, {j, 3}]

where $f$ and $g$ are two different functions, $t$ is a vector or List containing values of time, and $x$ is a variable number.

This provides with the following matrix :

$\small \begin{pmatrix} f[t[[1]],1,x] & g[t[[1]],1,x] & f[t[[1]],2,x] &g[t[[1]],2,x] & f[t[[1]],3,x] & g[t[[1]],3,x]\\ f[t[[2]],1,x]&g[t[[2]],1,x]&f[t[[2]],2,x]&g[t[[2]],2,x]&f[t[[2]],3,x]&g[t[[2]],3,x]\\ f[t[[3]],1,x]&g[t[[3]],1,x]&f[t[[3]],2,x]&g[t[[3]],2,x]&f[t[[3]],3,x]&g[t[[3]],3,x]\\ f[t[[4]],1,x] & g[t[[4]],1,x] & f[t[[4]],2,x] &g[t[[4]],2,x] & f[t[[4]],3,x] & g[t[[4]],3,x]\\ f[t[[5]],1,x] & g[t[[5]],1,x] & f[t[[5]],2,x] &g[t[[5]],2,x] & f[t[[5]],3,x] & g[t[[5]],3,x]\\ f[t[[6]],1,x] & g[t[[6]],1,x] & f[t[[6]],2,x] &g[t[[6]],2,x] & f[t[[6]],3,x] & g[t[[6]],3,x]\\ f[t[[7]],1,x] & g[t[[7]],1,x] & f[t[[7]],2,x] &g[t[[7]],2,x] & f[t[[7]],3,x] & g[t[[7]],3,x]\\ f[t[[8]],1,x] & g[t[[8]],1,x] & f[t[[8]],2,x] &g[t[[8]],2,x] & f[t[[8]],3,x] & g[t[[8]],3,x]\\ f[t[[9]],1,x] & g[t[[9]],1,x] & f[t[[9]],2,x] &g[t[[9]],2,x] & f[t[[9]],3,x] & g[t[[9]],3,x]\\ f[t[[10]],1,x] & g[t[[10]],1,x] & f[t[[10]],2,x] &g[t[[10]],2,x] & f[t[[10]],3,x] & g[t[[10]],3,x]\\ f[t[[11]],1,x] & g[t[[11]],1,x] & f[t[[11]],2,x] &g[t[[11]],2,x] & f[t[[11]],3,x] & g[t[[11]],3,x]\\ f[t[[12]],1,x] & g[t[[12]],1,x] & f[t[[12]],2,x] &g[t[[12]],2,x] & f[t[[12]],3,x] & g[t[[12]],3,x] \end{pmatrix}$

Now, in my experiments, I've found out that I don't need to calculate every value of the matrix but only four elements for each column, because the other ones are equal to zero. So I need to obtain the following matrix :

$\small \begin{pmatrix} f[t[[1]],1,x] & g[t[[1]],1,x] & 0&0 & 0 & 0\\ f[t[[2]],1,x]&g[t[[2]],1,x]&0&0&0&0\\ f[t[[3]],1,x]&g[t[[3]],1,x]&f[t[[3]],2,x]&g[t[[3]],2,x]&0&0\\ f[t[[4]],1,x] & g[t[[4]],1,x] & f[t[[4]],2,x] &g[t[[4]],2,x] & 0 & 0\\ 0 & 0 & f[t[[5]],2,x] &g[t[[5]],2,x] & f[t[[5]],3,x] & g[t[[5]],3,x]\\ 0 & 0 & f[t[[6]],2,x] &g[t[[6]],2,x] & f[t[[6]],3,x] & g[t[[6]],3,x]\\ 0 & 0 & 0 &0 & f[t[[7]],3,x] & g[t[[7]],3,x]\\ 0 & 0 & 0 &0 & f[t[[8]],3,x] & g[t[[8]],3,x]\\ 0 & 0 & 0 &0 & 0 & 0\\ 0 & 0 & 0 &0 & 0 & 0\\ 0 & 0 & 0 &0 & 0 & 0\\ 0 & 0 & 0 &0 & 0 & 0 \end{pmatrix}$

Is there a way to construct the matrix avoiding the calculus of the elements which are always equal to zero?

I tried this

Table[Sequence @@ {f[t[[i]], j, x], g[t[[i]], j, x]}, {i, 2j-1,2j+2}, {j, 3}]

but obviously without success...

My concern is with the time consumption (when working with large matrices).

$\endgroup$
  • 2
    $\begingroup$ Have you looked into SparseArray and Band?. I'm not totally sure, but those functions might be helpful here. $\endgroup$ – einbandi Feb 22 '13 at 17:35
11
$\begingroup$

For example:

m[p_] := SparseArray[Flatten@Table[{j, i} -> p[t[[i]], j, x], {i, 2 j - 1, 2 j + 2}, {j, 3}]];
h = Quiet@Transpose@Riffle[m[f], m[g]];
MatrixForm@h

Mathematica graphics

$\endgroup$
  • $\begingroup$ Thanks! It totally divided the time consumption by three! $\endgroup$ – jrojasqu Feb 22 '13 at 22:38
  • $\begingroup$ @jrojasqu Glad to hear that! :) $\endgroup$ – Dr. belisarius Feb 23 '13 at 5:18
  • $\begingroup$ Edit, for my particular case and algorithms, the time consumption was divided not by three but by ten! Thanks again! $\endgroup$ – jrojasqu Feb 23 '13 at 16:51

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.