How to get row and column from index?

For a zero-based index, the two operations are, where width is the width of the structure: row = index / width column = index % width Those are for C using integers, where the division rounds down and the % modulo operator gives the remainder. If you’re not using C, you may have to translate … Read more

Swap rows with columns (transposition) of a matrix in javascript [duplicate]

DuckDucking turned up this by Ken. Surprisingly, it’s even more concise and complete than Nikita’s answer. It retrieves column and row lengths implicitly within the guts of map(). function transpose(a) { return Object.keys(a[0]).map(function(c) { return a.map(function(r) { return r[c]; }); }); } console.log(transpose([ [1,2,3], [4,5,6], [7,8,9] ]));

Organizing felt tip pens: optimizing the arrangement of items in a 2D grid by similarity of adjacent items, using JS [updated]

I managed to find a solution with objective value 1861.54 by stapling a couple ideas together. Form unordered color clusters of size 8 by finding a min-cost matching and joining matched subclusters, repeated three times. We use d(C1, C2) = ∑c1 in C1 ∑c2 in C2 d(c1, c2) as the distance function for subclusters C1 … Read more