Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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).

share|improve this question
1  
Have you looked into SparseArray and Band?. I'm not totally sure, but those functions might be helpful here. –  einbandi Feb 22 '13 at 17:35
add comment

1 Answer

up vote 8 down vote accepted

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

share|improve this answer
    
Thanks! It totally divided the time consumption by three! –  jrojasqu Feb 22 '13 at 22:38
    
@jrojasqu Glad to hear that! :) –  belisarius Feb 23 '13 at 5:18
    
Edit, for my particular case and algorithms, the time consumption was divided not by three but by ten! Thanks again! –  jrojasqu Feb 23 '13 at 16:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.