0

This is a question in Maple. I understand in terms of java that I want a count and an increment but my logic doesn't convert as simply to maple code.

I have an very long list of numbers LIST (196) that I wish to turn into a 14x14 Array but using the convert(LIST,Array) only gives me a 1 dimensional array.

In Maple code, this will give me my first column.

j:=1; 
for i from 1 to 14 do
B[i,j]:=Longlistvalue[i]; 
end do;

It's clear that my second column comes from t=2 and s from 15 to 24 but I'm struggling to put this into a loop.

Surely there is either a loop I can use for this or a maple command that puts the first 14 into the first row (or column) then the next 14 into the next row/column etc?

My most recent attempt gets me

B:=Array(1..14,1..14):
n:=1;
m:=14;
for j from 1 to 14 do
  for i from n to m do
     B[i,j]:=Longlistvalue[i];
  end do;
  n:=n+14;
  m:=m+14;
end do;

But not it states that my array is out of range (because the s in B[i,j] must be less than 15).

Is there a way to get around this by means of a more efficient loop?

1 Answers1

1

The Array (or Matrix) constructor can be used to do this directly, using an operator to assign the entries.

You can lay the list entries down into the Array either by column or by row. Adjust the example to fit your case where m=14 and n=14.

m,n := 3,4:

L:=[seq(i,i=1..m*n)]; # you got your list in another way

             L := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Array(1..m,1..n,(i,j)->L[(j-1)*m+i]);

                          [1    4    7    10]
                          [                 ]
                          [2    5    8    11]
                          [                 ]
                          [3    6    9    12]

Array(1..m,1..n,(i,j)->L[(i-1)*n+j]);

                         [1     2     3     4]
                         [                   ]
                         [5     6     7     8]
                         [                   ]
                         [9    10    11    12]

You could also use nested loops.

Longlistvalue:=[seq(i,i=1..14^2)]: # your values will differ, of course

B:=Array(1..14,1..14):
n:=14;
m:=14;
for j from 1 to m do
   for i from 1 to n do
      B[i,j]:=Longlistvalue[(j-1)*m+i];
   end do;
end do:

# so we can see the contents, displayed in full
interface(rtablesize=infinity):

B;
acer
  • 6,671
  • 15
  • 15