A
A
Alexey Nikolaev2016-03-26 00:51:54
Programming
Alexey Nikolaev, 2016-03-26 00:51:54

How to calculate the position of an array element in a matrix based on its index and the dimension of the matrix?

Goodnight.
There is an array of elements (in fact, records from the database), and there is a number of elements in the line - N. In the loop, we go through the array, if the current element (i) is equal to N, we start a new line. In general, a typical picture for a huge number of sites.
I want to make it possible to display elements with a different order - not in a row, but in a column, but at the same time preserving the html \ css structure (flex disappears, thus). For example, if in the inline version the second element actually had an index of 2 (i.e. it was in the second place in the line), in the column version it will have an index of 5, and it will be located at the beginning of the second row (i.e. the second in the first column) . However, for example, if we have 10 elements and N is 4m, then the 6th element will in fact remain the sixth, so it is very problematic to guess arithmetically.
So far, I see only an option to convert the array to a matrix, and then rotate it 90 degrees, or even get smarter with nested loops, but this is too cumbersome for such a simple task. I want a beautiful and elegant solution, without nested loops and unnecessary transformations, just based on the available data.
Which option would you use, perhaps there are special formulas / algorithms for this? .. Not necessarily a ready-made example, you can write an article describing a possible way to solve this problem.
I would be grateful for advice, thanks.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly Pukhov, 2016-03-26
@Neuroware

I read it twice and still didn’t understand what you are trying to do, at least draw a picture chtoli or reformulate it simpler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question