multiplying 2 arrays in codesys

2022-08-08
2022-08-29
  • nehaltrivedi - 2022-08-08

    Hello i want to multiply 2 arrays for one of my projects which i am currently working on.

    Here is the code which i am trying to run:

    Program POU
    VAR
    a: array [1..5,1..2] of real:= [1,2,3,4,5,6,7,8,9,1];
    b: array [1..2,1..5] of real:= [1,2,3,4,5,6,7,8,9,1];
    c: array [1..5,1..5] of real;
    END_VAR

    How can this be done? It would be great if someone can help me

     

    Last edit: nehaltrivedi 2022-08-22
  • i-campbell

    i-campbell - 2022-08-08

    Try the Matrix Library, available by searching Matrix Library in CODESYS Installer > Change > Browse

     
    • nehaltrivedi - 2022-08-08

      But i checked and it seems isn't this library a paid one?
      And can't we multiply without matrix library?

       
  • i-campbell

    i-campbell - 2022-08-08

    I don't know of another way. I guess you could find an algorithm and convert it manually to IEC 61131-3.

     
  • ojz0r - 2022-08-09

    Since you already got the values in arrays cant you just loop through them?

     
    πŸ‘
    1
    • nehaltrivedi - 2022-08-17

      I am not able to identify what you are trying to tell, would be great if you can explain.

       
      • ojz0r - 2022-08-18

        Maybe i do not understand how you want them multiplied?
        I was thinking of something like this:

        c := 1;
        for i := 1 to 2 do
          for j := 1 to 5 do
            c := c * a[j] * b[j];
          end_for;
        end_for;
        
         

        Last edit: ojz0r 2022-08-18
        • nehaltrivedi - 2022-08-18

          I wanted to multiply the matrix a and matrix b which yields the result c = a*b.

           
          • ojz0r - 2022-08-18

            But C would be a new array, or do you want the array elements of C summed up?

             
            • nehaltrivedi - 2022-08-22

              Yes sorry C would be a new array. I want something like this c=a*b; I have edited the original problem statement.

               
              • ojz0r - 2022-08-27

                Okay so here is the loop(s) needed for the calculation:

                for i := 1 to 5 do
                  for j := 1 to 5 do
                    for k := 1 to 2 do
                      c[k,i] := c[k,i] + (a[k,j]*b[j,i]);
                    end_for;
                  end_for;
                end_for;
                
                 
                • nehaltrivedi - 2022-08-29

                  Do i need to initialise C[k,i] to 0's?
                  Something like this:
                  c: array [1..5,1..5] of real := [25(0)];

                   
                  • ojz0r - 2022-08-29

                    Initializing to zero is always a good idea to be sure variables are empty before a calculation.

                     

Log in to post a comment.