In this lesson, we’ll traverse 2-D arrays differently and map them to a 1-D array. Mapping here refers to the process of taking elements from a two-dimensional matrix and rearranging them in a specific order to form a one-dimensional array. The specific order will be determined by the direction specified by an arrow in the question. This direction can be row-wise, column-wise, diagonally, or any other specific direction.

For example, a row-wise mapping function would take the elements of the 2-D matrix and arrange them in the order in which they appear in each row. A column-wise mapping function, on the other hand, would arrange the elements in the order in which they appear in each column.

Traversals and mappings from R2R^2 to RR world

Mapping is a way to transform complex data structures into simpler data structures. In this case it transforms a 2-D matrix into a 1-D array, making it easier to process or manipulate.

Given a rows x cols two-dimensional matrix TDA, write the mapping functions from R2R^2 (two dimensions) to RR (one dimension) for each different problem.

Implement the mapping function for each different mapping case below to convert a two-dimensional matrix TDA with dimensions of rows x cols to a one-dimensional array projectionMap. The functions should take the elements of the 2-D array and arrange them in a specific order as indicated by the direction specified by an arrow.

Playground for your implementation

Write your implementation of the mapping function here.

Get hands-on with 1200+ tech skills courses.