1
$\begingroup$

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.

$\endgroup$
4
  • 2
    $\begingroup$ It is not quite clear what exactly you are asking for, but I am quite positive that Table and Do will help you. $\endgroup$ – Henrik Schumacher Jun 2 '18 at 8:32
  • 2
    $\begingroup$ k[[{1,9},{1,9}]] might also do it (look up Part). Note that Print just prints and does not create a new matrix. $\endgroup$ – Henrik Schumacher Jun 2 '18 at 9:24
  • $\begingroup$ i knw but can i not make any changing in my code to print a matrix form $\endgroup$ – muhammad asif Jun 2 '18 at 9:39
  • 2
    $\begingroup$ Why should I avoid the For loop in Mathematica? $\endgroup$ – corey979 Jun 2 '18 at 21:19
5
$\begingroup$

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]]
$\endgroup$
5
  • 1
    $\begingroup$ ...or m1[[{1, 9}, {1, 9}]].. $\endgroup$ – halirutan Jun 2 '18 at 13:24
  • 1
    $\begingroup$ ... or m1[[1;;9;;8,1;;9;;8]]... $\endgroup$ – Henrik Schumacher Jun 2 '18 at 16:02
  • $\begingroup$ @halirutan . Sure, but Henrik already mentioned that one in a comment. $\endgroup$ – m_goldberg Jun 2 '18 at 22:44
  • $\begingroup$ @HenrikSchumacher. Yeah, that, too. There are probably more. $\endgroup$ – m_goldberg Jun 2 '18 at 22:47
  • $\begingroup$ Waht i do with your code if i want to extract 4*4 matrix just like that ConstantArray[0, {4, 4}]; For[i = 1; ii = 1, i <= 9, i += 8; ii += 1, For[j = 1; jj = 1, j <= 9, j += 8; jj += 1, For[k = 3; kk = 3, k <= 11, k + 8; kk + 1, For[l = 3; ll = 3, l <= 11, l + 8; ll + 1, m2[[ii, jj, kk, ll]] = m[[i, j, k, l]]]]]]; m2 // MatrixForm $\endgroup$ – muhammad asif Jun 3 '18 at 10:24
2
$\begingroup$

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*)
$\endgroup$
4
  • $\begingroup$ thanks sirr but a want a changing in for loop to create matrix .. any idea $\endgroup$ – muhammad asif Jun 2 '18 at 9:35
  • $\begingroup$ Actually i have to evaluate a fomulla for different values so i need as input matrix $\endgroup$ – muhammad asif Jun 2 '18 at 9:37
  • $\begingroup$ 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! $\endgroup$ – Rajendra prasad Jun 2 '18 at 9:47
  • $\begingroup$ 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 $\endgroup$ – muhammad asif Jun 2 '18 at 9:52
0
$\begingroup$

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, #] &
$\endgroup$

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