A
A
Alexey Nikolaev2016-03-27 13:44:09
Programming
Alexey Nikolaev, 2016-03-27 13:44:09

How to form an already rotated matrix from an array?

Good afternoon.
I have a one dimensional array. Array elements are output several per line, up to the specified limit, then a new one begins (similar to array_chunk($array, 3) - i.e. a matrix is ​​created).
It is necessary in this cycle to create a matrix already rotated 90 degrees clockwise, without nested loops and other things that increase the complexity of the algorithm. Scheme of what I want to do: option 2 - what you need to get in O (n) from the original array.

spoiler
08abe76e355e4c358844a9d3f58333ab.jpg

My working version , on js. What do you want to do in the end:
var array = ['1','2','3','4','5','6','7','8','9','10','11','12'];

for(var elems = 0; elems < array.length; elems++) {
    var index = ?; // расчет индекса, как будто он находится в повернутой матрице (вариант 2). Т.е. 2 = 6, 3 = 9 и т.п.
}

I tried a variety of calculation logic, it did not work without nested loops.
I would be glad for any advice on the topic (including a general description of a possible algorithm), thanks.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Bykov, 2016-03-27
@Heian

The formula for the transformation shown in the figure (i.e., not a 90 degree rotation, but a transposition):
index = 1 + [i / m] + n * (i % m)
In this formula, index i changes from 0 to size - 1 (size - array size), n - number of columns (and after transformation - rows), m - number of rows (and after transformation - columns), [i / m] - integer part of dividing i by m, i % m - remainder after dividing i by m.
Obviously, m = [ (size - 1) / n ] + 1
With size = 12, n = 4 (respectively, m = 3), we get the following sequence of index values:

i     [i / 3]    4 * (i % 3)    index
0:  1 +  0    +     4 * 0    =    1
1:  1 +  0    +     4 * 1    =    5
2:  1 +  0    +     4 * 2    =    9
3:  1 +  1    +     4 * 0    =    2
4:  1 +  1    +     4 * 1    =    6
5:  1 +  1    +     4 * 2    =   10
6:  1 +  2    +     4 * 0    =    3
7:  1 +  2    +     4 * 1    =    7
...

, i.e., as in the example of a working option

D
Dmitry Belyaev, 2016-03-27
@bingo347

In the figure, the matrix is ​​\u200b\u200breflected diagonally.
But still, the task sounds like rotated 90 degrees clockwise, here is the algorithm:

//Исходные данные
var arr = ['1','2','3','4','5','6','7','8','9','10','11','12'];
var xlen = 4; //длина строки, или в повернутой матрице столбца

//Предварительные вычисления
var ylen = Math.ceil(arr.length / xlen); //Длина столбца
var matrix = new Array(xlen); //Строки результата
//Заполним строки массивами ячеек
for(var i = xlen; i--;) {
    matrix[i] = new Array(ylen);
}

//я пробегаю массив в обратном порядке
//так как это более оптимально по скорости
for(var i = arr.length; i--;) {
    //Вычислим координаты из i
    var x = ylen - 1 - Math.floor(i / xlen);
    var y = i % xlen;
    //и заполним матрицу
    matrix[y][x] = arr[i];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question