Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Matrix addition (MPLAB)

Status
Not open for further replies.

Mike.B

New Member
Hello everyone !

I have a trouble with my program and I don't know why.
Can you help me ?

I have MPLAB 8.92 + CSS and I have a PICKIT3.
So I would like to sum two matrices (and later, program a matrix multiplication).

My first code is about Integer matrices. It works very well.


Code:
#include <18F45K20.h>




void sum(int **mat1,int **mat2, int **res){
int i=0;
int j=0;
int k=0;

for(i=0;i<2;i++){
for(j=0;j<2;j++){
*(res+(1*i)+j)=*(mat1+(1*i)+j)+*(mat2+(1*i)+j);
}
}
}



int m1[2][2]={{15,30},{20,21}};
int m2[2][2]={{1,0},{0,1}};
int result[2][2];

void main()
{

sum(m1,m2,result);

while(1);
}


I get : result[2][2]={{16,30},{20,22}}, that's correct.

Now, I would like to do the same think but not with Integer but with Double.
So, I change the "int" by "double" : it doesn't work.

Here is the code :

Code:
#include <18F45K20.h>




void sum(double **mat1,double **mat2, double **res){
int i=0;
int j=0;
int k=0;

for(i=0;i<2;i++){
for(j=0;j<2;j++){
*(res+(1*i)+j)=*(mat1+(1*i)+j)+*(mat2+(1*i)+j);
}
}
}



double m1[2][2]={{15.0,30.0},{20.0,21.0}};
double m2[2][2]={{1.0,0.0},{0.0,1.0}};
double result[2][2];

void main()
{

sum(m1,m2,result);

while(1);
}

I get : result[2][2]={{16.0,0.0},{0.0,0.0}};

I don't understand why. Where is the problem ?

If someone could help, It would be wonderful.

Thanks and have a nice day !
 
It is not surprising that it doesn't work for doubles. It is surprising it works for ints.

"int **x" type means pointer to a pointer pointing to int, as "int *(*x)". It doesn't mean a pointer to an array of ints as "int (*x)[2]"
 
Alright, thanks for your answer ! Pointers are something new to me...
By using, "int *x" or "double *x", it works perfectly !

Just one more question :
Why can we say that : matrice[j] is equivalent to *(matrice+(number of columns*i)+j). I don't get the point.
Can you explain me ?
 
Imagine all the elements of the matrix are stored in memory, linerarly, one by one. When you add 1 to a pointer, it shifts one position to the right, so if "matrice" pints to an element of the array, (matrice+1) will point through the next element. "matrice" points to the first element, (matrice+1) points to the second, (matrice+n) points to the (n-1)th element. In such use, n is often called "offset". Each element of the matrix has its own offset. You simply calculate the offset where (i,j) element is - (number of columns*i)+j and add it to "matrice". Now it points to the desired element. *(something) means you want to take an element pointed by "something". When you apply it to the pointer to an element, it fetches the element pointed to by the pointer.

Note, that if you use one-dimentional array, it's up to you wether you want it by rows or by columns (number of rows*j)+i will work just as well as (number of columns*i)+j. You only need to make sure that there's enough memory for the matrix at the place where the pointer points to.

BTW: Number of columns in 2x2 matrix is 2, not 1.
 
Hello NorthGuy !

Sorry for taking so long to anwser. Something came up and I had to fix it first.
Anyway, thanks for your explanation. Some parts are already more understable to me. But I need more clarifications for other parts.

Imagine all the elements of the matrix are stored in memory, linerarly, one by one. When you add 1 to a pointer, it shifts one position to the right, so if "matrice" pints to an element of the array, (matrice+1) will point through the next element. "matrice" points to the first element, (matrice+1) points to the second, (matrice+n) points to the (n-1)th element. In such use, n is often called "offset".

I understand this part very easily if we talk about an one-dimentional array. In fact, (matrice+n) can be compared to (matrice[n]).
I can picture that in my head :)

However, if we talk about matrices (two-dimentional arrays), I am getting confused a little.
Indeed, how can all the elements of the matrix be stored in memory, linerarly, one by one ?
Let's consider a matrix with two columns. Does it means the second column would be under the first one in the memory ?

(number of columns*i)+j and add it to "matrice"
Why (number of columns*i)+j and not (i+j) ?

*(something) means you want to take an element pointed by "something". When you apply it to the pointer to an element, it fetches the element pointed to by the pointer.
So you can get the element pointed value ? What about "&" operator ?

Thanks
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top