D
D
Denis2021-07-21 11:51:20
C++ / C#
Denis, 2021-07-21 11:51:20

Matrix within a matrix?

How to create a matrix where one of its elements is the second matrix?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
rPman, 2021-07-21
@NeYmen

In c++ there is a notation mathematically similar to accessing matrix elements in mathematics:
m[x][y] - i.e. we take the element xy of the matrix m (formally, on the contrary, of course, first the Y rows then the X columns, but in the end there is no difference, except if optimization is important).
In reality, this entry means that we take the x-th array in the list m and take the y-th element from the resulting array, i.e. m is of type array of arrays of elements , and to work with it, it must be initialized by creating the required number of arrays of the same dimension.
ps sometimes, when you want 'simplicity' of initialization, reduction of memory fragmentation, more efficient access to sequential elements, you can store an array as a one-dimensional array of all matrix elements, and access the elements by calculating the offset in this array as x+y*N, where N is the size of the matrix in X, i.e. m[x+y*N], but when you need to work sequentially with all elements from left to right, from top to bottom, you can simply work by offset in this one-dimensional array m[i++]
Now further - create an array with dimension M (number of matrix rows) , pointers to arrays of elements, and initialize it with pointers to every Nth element in this one-dimensional array of elements, and we get the opportunity to work with a one-dimensional array as a two-dimensional syntactically, while maintaining the possibility of fast element-by-element access...

const int N=3;
const int M=2;
int ml[N*M]={1,1,1,2,2,2};
// матрица:
// 111
// 222
int* m[M];
for(int i=0;i<M;i++) m[i]=*(ml[i*N]);
//
for(int i=0;i<M*N;i++) ml[i]...
и
for(int y=0;i<M;i++) 
  for(int x=0;i<N;i++) 
    m[y][x]... // порядок колонки строки тут перевернут, но можно заранее это учитывать и перевернуть везде в коде для удобства восприятия

pps maybe I made a mistake somewhere, but it doesn’t matter, I haven’t written in C for a hundred years

A
Antony, 2021-07-21
@RiseOfDeath

What is a matrix in C/C++ ?
There are no matrices in C/C++, it's a mathematical concept that can be implemented as just a "two-dimensional" array. More precisely, this is either a one-dimensional array, which, as we believe, has a new "string" every N elements, or a one-dimensional array of "strings", each element of which is a pointer to a one-dimensional array-string.
What can be an array element? Anything, such as a pointer to anything, such as another array from the previous question (which we interpret as a matrix).
As for pure C++ emnip, there are no standard objects for 2-dimensional or N-dimensional arrays in stl, but one-dimensional arrays can be used in the above way (unfortunately, I don’t know if there are multidimensional arrays in the boost, but most likely not either)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question