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.

We have 3 matrices: m1, m2 and b. We want to apply matrix operations (dot or sum) in a particular way: we want to treat the columns of m2 and b separately as a vectors and m1 as a matrix.

m1 is a (9*9) matrix:

m1 = {{0, 0.25, 0, 0.25, 0, 0, 0, 0, 0}, {0.25, 0, 0.25, 0, 0.25, 0, 0, 0,0},
      {0, 0.25, 0, 0, 0, 0.25, 0, 0, 0}, {0.25, 0, 0, 0, 0.25, 0, 0.25, 0,0},
      {0, 0.25, 0, 0.25, 0, 0.25, 0, 0.25, 0}, {0, 0, 0.25, 0, 0.25, 0, 0,0, 0.25},
      {0, 0, 0, 0.25, 0, 0, 0, 0.25, 0}, {0, 0, 0, 0, 0.25, 0, 0.25, 0, 0.25},
      {0, 0, 0, 0, 0, 0.25, 0, 0.25, 0}}

enter image description here

m2 and b are (9*15) and (9*9) matrices, respectively, with the elements:

m2 = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
     {0, 0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
     {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},   
     {0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0}, 
      {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
     {0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}

b = {{1.5, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 1.5, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0,0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},
    {0, 0, 0, 0, 0, 0, -1.5, 0, 0}, {0, 0, 0, 0, 0, 0, 0, -1, 0}, 
    {0, 0, 0, 0, 0, 0, 0, 0, -1.5}}

We want to be able to extract any column of m2 and b as a vector

enter image description here enter image description here

in a such way, that we will be able to form the product m1 with any extracted vector of m2 and add it to an extracted column of b in the following way:

column[2][m2] = m1.column[1][m2] + column[1][b]

column[3][m2] = m1.column[2][m2] + column[2][b]

column[4][m2] = m1.column[3][m2 ]+ column[3][b]

column[i][m2] = m1.column[i - 1][m2] + column[i - 1][b]

and so on.

Finally we wanted to populate the matrix m2 with the newly calculated elements instead of zeros.

share|improve this question
    
This seems to be precisely what you would get by just doing m1.m2 + b and extracting the columns of the result. –  Jens Jun 24 at 3:19
    
With this way, all result will be 'b' because at the first step, m2 is a zero matrix. As long as I want to get column[i][m2]=m1.column[i-1][m2]+column[i-1][b] –  mostafa Jun 24 at 3:21
    
Actually, m2 vanishes. So the result can be taken to be the columns of b alone, right? –  Jens Jun 24 at 3:25
    
After the first step, the first column of m2 will not be zero and some elements of b fills them, for calculating the second column of m2 we need the first column (of m2) which is not zero at this step then m2 can not be vanished in the continuous process. –  mostafa Jun 24 at 3:30
    
Oh, I get it. So m2 is the "storage" for the results... –  Jens Jun 24 at 3:33

1 Answer 1

up vote 3 down vote accepted

Your algorithm is reproduced by this line:

m2 = Transpose[FoldList[m1.#1 + #2 &, {0, 0, 0, 0, 0, 0, 0, 0, 0},  
      Transpose[b]]];

MatrixForm[m2]

matrix

Here I use FoldList to implement the iterative application of m1 to a vector, starting with zero ({0, 0, 0, 0, 0, 0, 0, 0, 0}, which is the first column of m2 to begin with and will also be the first column in the result), and the addition of successive columns of b. The columns of b are first converted to rows by Transpose so that each original column is an element in a list that can be fed to FoldList as the last argument.

In the matrix operation m1.#1 + #2 &, the #1 stands for the vector on which m1 acts. The resulting vector is a row vector (i.e., a one-level List). The second argument #2 is also a row vector obtained from Transpose[b], i.e., a column of b. The FoldList command collects the resulting row vectors in a new List of rows. But finally we want this list of rows to be represented as columns of a matrix. This is what the outermost Transpose is for.

The important thing to understand is FoldList. It iterates exactly as many times as there are elements in the last argument, Transpose[b]. That is, in our case, nine times. This gives you the freedom to have an arbitrary number of columns in b. Since m2 is the matrix that stores the iterative results, it can have only as many columns as there are iterations, so its dimension is not $9\times 15$ if b has $9$ columns. Instead, the column dimension of m2 is then $10$ because the first column is the original zero vector.

share|improve this answer
    
thank you so much for your immediate response, please let me check for my case. –  mostafa Jun 24 at 5:17

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.