# Matrix operations

• I'd like to create a class for an electronic project based on a matrix of leds, like the Adafruit Led Backpack. The project is a graphical editor for all the leds for a backpack of any size.

Therefore, I've thought of QMatrix 4x4 since under this size no physical matrix exists.

After some searches Qmatrix4x4 seems strongly related to a 4x4 matrix -ie : overloading seems requiring a lot of work- and QGenericMatrix has to few functions .
Besides, using an external lib like Eigen is overkilled as I need basic matrix capabilities.

• QMatrix 4x4 is used for 3d operations, it's not a container.

if by "basic matrix capabilities" you mean you don't even need linear algebra any container works. for example

• `std::vector<std::vector<T> >` 2D matrix of type `T` 1 of the dimension can have different sizes (e.g. different rows can have different number of columns)
• `std::vector<std::array<T,N> >` 2D matrix of type `T` 1 of the dimensions size (`N`) is fixed at compile time
• `std::array<std::array<T,N> M>` 2D matrix of type `T` both dimension sizes (`N`,`M`) are fixed at compile time
• `boost::multi_array<T, D>;` multi dimensional (`D`=dimensions) "matrix" of type `T`, all dimensions sizes are fixed at runtime (set in the constructor)

• @VRonin

Now I understand that this is not a "standard" container as I thought, indeed .

By "Basic Matrix Capabilities" I meant a little more than storing like vector or array do, it was more a comparison to big math. libraries like Eigen or Blaze.
Actually I'd need some minimal matrix functions like rotation, inverse, flip

• Hi! For simple stuff in 2D, QPointF and QTransform should be good enough.

• @Wieland

I was so happy about the QTransform which was really close to the solution....till I found :

"QTransform differs from QMatrix in that it is a true 3x3 matrix, "

• And what's the problem with that? :)

• @Wieland

I need to be able to have different matrix sizes : The project is a graphical editor for all the leds for a backpack of any size.

• Okay, sorry, seems like I don't understand what kind of maths you intent to do with it. I thought you needed some basic transforms, like rotation, scale, and such.

• @Wieland
That's right : basic tranform, rotation, scale are fine. But size 3x3 is not enough: the one I have like the link I provide in my example is a 8x8

• @doubitchou Just out of curiosity, can you provide some equations? Because I really don't get what you mean when you have a 2D problem and you say you want to rotate a 8x8 matrix.

• @Wieland

As a use case let's consider you have the 8x8 leds backpack which can have 3 values (representing colors in that case)

0 : off
1 : Green
2 : Red
3 : Orange (Green + Red btw)

Therefore, if I want to draw my 1st line as Red and the Second as Green :

22222222
11111111
00000000
00000000
00000000
00000000
00000000
00000000

And when I want to rotate right it would be :

00000012
00000012
00000012
00000012
00000012
00000012
00000012
00000012

• Oh, now I get it. This has nothing to do with the usual rotate, translate, scale,... transforms. There are no functions for this readily available in our framework. You need to implement that all by yourself.

• @Wieland said in Matrix operations:

Oh, now I get it. This has nothing to do with the usual rotate, translate, scale,... transforms. There are no functions for this readily available in our framework. You need to implement that all by yourself.

Indeed this is what I was just thinking ... since the VRonin answer.

Thanks to both of you

• What you described is called a matrix transposition. My advice - Just use Eigen, it's headers-only anyway.

• @kshegunov said in Matrix operations:

What you described is called a matrix transposition. My advice - Just use Eigen, it's headers-only anyway.

Oh ? Great I was about implementing all by myself.
Thanks for this

• @doubitchou said in Matrix operations:

Actually I'd need some minimal matrix functions like rotation, inverse, flip

if by rotation and inverse you don't mean the mathematical terms linked but physical movements of the image in the leds, all these operations can be easily done just by acting on the indexes rather than the content of the matrix.

For example:
given a `matrix[rowCount][colCount]`

`matrix[i][j]`

• mirrored on the vertical axis is `matrix[i][colCount-j-1]`
• rotated 90° counter-clockwise is `matrix[j][i]`

• To provide a better understanding I've put some design concepts :

As you can see these are basic operations

• You could even use `QImage` with `QImage::Format_Mono` format as a container actually

• @VRonin said in Matrix operations:

You could even use `QImage` with `QImage::Format_Mono` format as a container actually

I'm not sure to understand it right

• `QImage myMatrix(8,8,QImage::Format_Mono)` will create a container of 8 by 8 bits (not bytes).

• `myMatrix.fill(0);` to turn everything off.
• Point: `myMatrix.setPixel(0,0,1);`
• Line(1): `for(int i=0;i<myMatrix.width();++i) myMatrix.setPixel(0,i,1);`
• Column(1): `for(int i=0;i<myMatrix.height();++i) myMatrix.setPixel(i,0,1);`
• Border: `for(int i=0;i<myMatrix.width();++i) {myMatrix.setPixel(0,i,1); myMatrix.setPixel(myMatrix.height()-1,i,1);} for(int i=0;i<myMatrix.height();++i) {myMatrix.setPixel(i,0,1); myMatrix.setPixel(i,myMatrix.width()-1,1);}`
• Column Shift(1): `myMatrix=myMatrix.transformed(QTransform().translate(1,0));`
• Column Shift(-1): `myMatrix=myMatrix.transformed(QTransform().translate(-1,0));`
• Line Shift(1): `myMatrix=myMatrix.transformed(QTransform().translate(0,1));`
• Line Shift(-1): `myMatrix=myMatrix.transformed(QTransform().translate(0,-1));`
• Rotate(1): `myMatrix=myMatrix.transformed(QTransform().rotate(270));`
• Rotate(-1): `myMatrix=myMatrix.transformed(QTransform().rotate(90));`
• Invert: `myMatrix.invertPixels();`

• @VRonin

Great answer !, especially to be adapted to my needs. Anyway, I think I should try with Format_Indexed8 instead as I need to store the color values which requires more than a single bit:

0 : off
1 : Green
2 : Red
3 : Orange

(Merci pour cette réponse)