1

I have the following code:

For[i = 1, i <= 9, i = i + 8,
  For[j = 1, j <= 9, j = j + 8,
    Print[k[[i, j]]]
  ]
]

The code prints a column of the elements that I want to extract, but I want them as a 2 x 2 matrix. How can I modify my program so it does that?

I want to use For-loops to form the 2 x 2 matrix.

m_goldberg
  • 107,779
  • 16
  • 103
  • 257

3 Answers3

5

Contrived test data.

m1 = Array[k, {9, 9}]
{{k[1, 1], k[1, 2], k[1, 3], k[1, 4], k[1, 5], k[1, 6], k[1, 7], k[1, 8], k[1, 9]}, 
 ... 
 {k[9, 1], k[9, 2], k[9, 3], k[9, 4], k[9, 5], k[9, 6], k[9, 7], k[9, 8], k[9, 9]}}

Here is how to solve your problem with For-loops. m2 will be the matrix you want. You can substitute any 9 x 9 matrix for m1 and the code will still work.

m2 = ConstantArray[0, {2, 2}];
For[i = 1; ii = 1, i <= 9, i += 8; ii += 1,
  For[j = 1; jj = 1, j <= 9, j += 8; jj += 1,
    m2[[ii, jj]] = m1[[i, j]]]];
m2 // MatrixForm

matrix

But really, wouldn't it be simpler just to write

m2 = {m1[[1, 1]], m1[[1, 9]], m1[[9, 1]], m1[[9, 9]]}

or

m2 = {m1[[1, 1]], m1[[1, -1]], m1[[-1, 1]], m1[[-1, -1]]}

or perhaps

m2 = Extract[m1, Tuples[{1, 9}, 2]]

or

m2 = Extract[m1, Tuples[{1, -1}, 2]]
m_goldberg
  • 107,779
  • 16
  • 103
  • 257
2

There are different ways to achieve this. The solution differs based on what one wants exactly. If the element's positions are combinations of m and n in the list, the crude way of achieving this is:

Table[t[[i,j]],{i,{m,n}},{j,{m,n}}]

In the above, t is the list that contains the elements. This will generate the list of the elements required. As the output is required in the matrix form,

Table[t[[i,j]],{i,{m,n}},{j,{m,n}}]//MatrixForm

will print the list in the matrix form.

Update: Let A is the list that contains the extracted elements, B is the identity matrix that has to be added and m and n are the elements in the indices

A=Table[t[[i,j]],{i,{m,n}},{j,{m,n}}] (*extracts the elements from the list t from the postions, {m,m},{m,n},{n,m} and {n,n}*)
B=IdentityMatrix[2];  (*generates identity matrix of size 2*)
C=A+B;   (*compltes addition of lists and stores in C *)
Print[MatrixForm[C]]  (*Prints C in the matrix form*)
  • thanks sirr but a want a changing in for loop to create matrix .. any idea – muhammad asif Jun 02 '18 at 09:35
  • Actually i have to evaluate a fomulla for different values so i need as input matrix – muhammad asif Jun 02 '18 at 09:37
  • If you want it to be used in subsequent parts of the code, this can be stored as another variable and can be used.

    Unless it is clearly stated what exactly wanted, it is difficult to help with the code!

    – Rajendra prasad Jun 02 '18 at 09:47
  • okk sirr problem is that i extract elements using above code appear like a columns but i want to make 2*2 matrix because i have to add these elments with identity matrix.. i think you will b clrear now – muhammad asif Jun 02 '18 at 09:52
0

If you insist in For and something similar to Print then

With[{prnt = WriteString[$Output, ##] &},
 For[i = 1, i <= 9, i = i + 8,
  For[j = 1, j <= 9, j = j + 8,
   prnt[k[[i, j]], " "]
   ];
  prnt["\n"]
  ]
 ]

From the documentation details for Print

With a text-based interface, Print ends its output with a single newline (line feed).

Print sends its output to the channel $Output.

And each Print will be in an independent cell, but we can WriteString to the same $Output using

WriteString[$Output, #] &
rhermans
  • 36,518
  • 4
  • 57
  • 149