Working with Matrices: Adding/Subtracting and Custom Operations

  • Thread starter Jarfi
  • Start date
  • Tags
    Matrices
In summary: So you might be able to just do something like this:*land[(i+housex):(i+housex+5)]=*house;But it may be more efficient to use a library that can do this for you.
  • #1
Jarfi
384
12
TL;DR Summary
How do I combine heatmaps, or images using simple matrix mathematics, in the simplest possible way?
Hello,

I am working with heatmaps and similar. And I want to be able to add/subtract them and a few other custom operations.

Here is a quick rundown:
1: Generate nxn (or nxm if possible) matrix, each element represents a heatmap color, or alternatively this could simply be defined as an image, let's call it M1

2: Generate another nxn matrix, but say with all elements transparents, except a certain area which represents a picture of an object, say a house. Alternatively, it can be an overlay of numbers, all 0 but in the area of interest, there are values 1-255, which can be a picture of something. Let's call it M2

3: I now want to do operations, such as M1 + M2 to get an overlay image, but not only this, I want to be able to control the change of basis or translate the position of the house in matrix M2 before adding it in M1, and do this multiple times, so I can add multiple houses onto the main image.

Main image = M1 + M2 * (location 1 transformation) + M2 * (location 2 transformation) so that I can add two houses to the matrix M1, on location 1 and location 2.

what is the most simple way to do this...? I assume the transformation can be defined in some way , so that all I need is matrix multiplication, so that most programming language can handle it?

But what if the house is size 50x50, and the main matrix is 500x500, but I put the house at location [0,250], so that half of the house would end up outside, but the other half still appears in the matrix, is this possible using some mathematical (non algorithmic) matrix transformation?
 
Technology news on Phys.org
  • #2
These actions are more about image processing than about matrix operations.

Basically, it seems you want to adjust pixels, overlay pixels with other pixels, rescale one image to overlay on another image.

There are software libraries and tools to do that. Java has some libraries for as does Python. ImageMagick comes to mind for commands and Gimp for interactive tooling.
 
  • Like
Likes sysprog
  • #3
jedishrfu said:
These actions are more about image processing than about matrix operations.

Basically, it seems you want to adjust pixels, overlay pixels with other pixels, rescale one image to overlay on another image.

There are software libraries and tools to do that. Java has some libraries for as does Python. ImageMagick comes to mind for commands and Gimp for interactive tooling.
It seems but it is not. I would like to do this without any clunky libraries, because it's very basic operations on nxn matrices, populated with simple numbers. It seems strange to me to have to revert to using libraries for this.

Again I am plotting heatmaps, not an rgb image
 
  • #4
These actions aren't really linear algebra though.

Basically you need to write code or use libraries (better ie already debugged) that can do these manipulations.

In the case of the house placement:

Java:
// Java based psuedo code overlaying house array onto 
// the land array (realtors and witches beware a house may fall on you)
int land[][] = new int[100][100]; // land area
int house[][] = new int[10][10]; // house

// load land array
// load house array

// house position on land
int housex=25; //x coordinate of top right corner of house image
int housey=35; // y coordinate of top right corner of house image

// overlay house onto land
for(int i=0; i<house.length; i++) {
  for(int j=0; i<house[0].length; j++) {
    land[i+housex][j+housey]=house[i][j];
  }
}

show(land);
 
  • Like
Likes sysprog
  • #5
jedishrfu said:
These actions aren't really linear algebra though.

Basically you need to write code or use libraries (better ie already debugged) that can do these manipulations.

In the case of the house placement:

Java:
// Java based psuedo code overlaying house array onto
// the land array (realtors and witches beware a house may fall on you)
int land[][] = new int[100][100]; // land area
int house[][] = new int[10][10]; // house

// load land array
// load house array

// house position on land
int housex=25; //x coordinate of top right corner of house image
int housey=35; // y coordinate of top right corner of house image

// overlay house onto land
for(int i=0; i<house.length; i++) {
  for(int j=0; i<house[0].length; j++) {
    land[i+housex][j+housey]=house[i][j];
  }
}

show(land);
This looks correct, and I have thought of doing this. But it also looks really computationally intensive. What If I want to make a moving house, for 30 fps, I need to do ixj=100 operations for each pixel, making 3.000 operations just for moving this single house. Or in case of a moving heamap (for example, a moving weather forecast showing the pressure degree, humidity or temperature) if I overlay more squares it might be 50x50x30 = 75.000 computations per second, the computation time increases exponantially with the size of the picture.

Perhaps it would be quicker to use pointers or something that switches out "blocks" of the matrix for another.
Pseudocode..
*land[(i+housex):(i+housex+5)][(j+housey):(j+housey+10)]=*house;

From my understanding, in computer graphics, they use graphical objects, where each one is basically a set of points connected together forming a polygon(mesh), then each object/matrix is transformed, rotated, translated etc and then added to the "main space" to make it move. There can be many objects, and together they form the image of whatever picture, video or graphics is being generated.
 
  • #6
ultimately Though those polygon meshes are converted to pixels and overlaid into your image by the graphics card.
 

1. What are matrices and how are they used in science?

Matrices are rectangular arrays of numbers or symbols used to represent data in a concise and organized format. They are commonly used in science to describe and analyze complex systems, such as chemical reactions, population dynamics, and physical systems.

2. How do you add and subtract matrices?

To add or subtract matrices, you simply add or subtract the corresponding elements in each matrix. The resulting matrix will have the same dimensions as the original matrices. It is important to note that the matrices being added or subtracted must have the same dimensions.

3. What are some common operations performed on matrices?

In addition to addition and subtraction, other common operations performed on matrices include multiplication, division, and finding determinants and inverses. Matrices can also be transformed using techniques such as transpose, scalar multiplication, and row reduction.

4. Can custom operations be performed on matrices?

Yes, custom operations can be performed on matrices by using user-defined functions. These functions can be programmed to perform specific operations on matrices, such as finding eigenvalues or eigenvectors, or solving systems of equations.

5. What are some real-world applications of matrix operations?

Matrix operations have numerous real-world applications in various fields of science and engineering. For example, in physics, matrices are used to model quantum mechanics and to analyze data from experiments. In genetics, matrices are used to study the relationships between different species. In computer graphics, matrices are used to transform and manipulate 3D images. In economics, matrices are used to model supply and demand and to analyze market trends.

Similar threads

  • Linear and Abstract Algebra
2
Replies
48
Views
5K
  • Linear and Abstract Algebra
Replies
11
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
5K
Replies
1
Views
1K
  • Linear and Abstract Algebra
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
1
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Replies
1
Views
3K
  • General Math
Replies
4
Views
1K
  • Linear and Abstract Algebra
Replies
1
Views
969
Back
Top