How can I print an associative array as a matrix

katy

I'm creating 2 associative arrays in which I put random values from 0 to 30, and after that I want to print then as a matrix. Is there a way I can do that?

Here is my code:

set serveroutput on
DECLARE 
    TYPE MyTab IS TABLE OF NUMBER INDEX BY VARCHAR2(10);
    mat1 MyTab;
    mat2 MyTab;
    v_n NUMBER(2);
    v_m NUMBER(2);
    v_nr NUMBER(3);
    v_dim NUMBER(3);
BEGIN
   v_n := round(dbms_random.value(2,5));
   v_m := round(dbms_random.value(2,5));
   v_nr := 1;
   v_dim := v_n*v_m;
   DBMS_OUTPUT.PUT_LINE(v_n||'    '||v_m);
   FOR i in 1 ..v_dim LOOP
       mat1(v_nr) := round(dbms_random.value(0,30));
       v_nr := v_nr+1;
   END LOOP;
   v_nr := 1;
   FOR i in 1 ..v_dim LOOP
       mat2(v_nr) := round(dbms_random.value(0,30));
       v_nr := v_nr+1;
   END LOOP;
   FOR i in 1 ..v_dim LOOP
       DBMS_OUTPUT.PUT_LINE(mat1(i));    
    END LOOP; 
   DBMS_OUTPUT.PUT_LINE(chr(10));
   FOR i in 1 ..v_dim LOOP
       DBMS_OUTPUT.PUT_LINE(mat2(i));    
   END LOOP;
END;
/
J. Chomel

I just understood there are 2 matrixes, mat1 and mat2, which have various size (but both have same dimensions). Here is how to display them:

set serveroutput on
DECLARE 
    TYPE MyTab IS TABLE OF NUMBER INDEX BY  pls_integer;
    mat1 MyTab;
    mat2 MyTab;
    v_n pls_integer;
    v_m pls_integer;
    v_nr pls_integer;
    v_dim pls_integer;
BEGIN
   v_n := round(dbms_random.value(2,5));
   v_m := round(dbms_random.value(2,5));
   if (v_n > v_m) then
       -- switch values for V_m to be the biggest dim
       v_nr:=v_n;
       v_n:=v_m;
       v_m:=v_nr;
   end if;

   v_nr := 1;
   v_dim := v_n*v_m;
   DBMS_OUTPUT.PUT_LINE(v_n||'    '||v_m);
   FOR i in 1 ..v_dim LOOP
       mat1(v_nr) := round(dbms_random.value(0,30));
       v_nr := v_nr+1;
   END LOOP;
   v_nr := 1;
   FOR i in 1 ..v_dim LOOP
       mat2(v_nr) := round(dbms_random.value(0,30));
       v_nr := v_nr+1;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE('MATRIX1');      
   FOR i in 1 ..v_n LOOP
       FOR j in 1 ..v_m LOOP
           DBMS_OUTPUT.PUT(' - '|| rpad(mat1((j-1)*v_n + i), 4));               
       END LOOP;
       DBMS_OUTPUT.PUT_LINE('');
    END LOOP; 

   DBMS_OUTPUT.PUT_LINE('MATRIX2');      
   FOR i in 1 ..v_n LOOP
       FOR j in 1 ..v_m LOOP
           DBMS_OUTPUT.PUT(' - '|| rpad(mat2((j-1)*v_n + i), 4));               
       END LOOP;
       DBMS_OUTPUT.PUT_LINE('');
    END LOOP; 
END;
/

I changed the types to pls_integer which looks more simple. Then keep in mind that I put the matrices in the good form where they can be multiplied (swith v_nand v_m in the loops, and added formatting to understand what happens.

DECLARE 
    TYPE MyTab IS TABLE OF NUMBER INDEX BY  pls_integer;
    mat1 MyTab;
    mat2 MyTab;
    v_n pls_integer;
    v_m pls_integer;
    v_nr pls_integer;
    v_dim pls_integer;
    idx pls_integer;
    idx1 pls_integer;
    idx2 pls_integer;
    v_p number;
BEGIN
   v_n := round(dbms_random.value(2,5));
   v_m := round(dbms_random.value(2,5));
--   v_n := 2; -- formating works better with 2 and 3
--   v_m := 3;
   v_nr := 1;
   v_dim := v_n*v_m;
   DBMS_OUTPUT.PUT_LINE(v_n||'    '||v_m);
   FOR i in 1 ..v_dim LOOP
       mat1(v_nr) := round(dbms_random.value(0,30));
       v_nr := v_nr+1;
   END LOOP;
   v_nr := 1;
   FOR i in 1 ..v_dim LOOP
       mat2(v_nr) := round(dbms_random.value(0,30));
       v_nr := v_nr+1;
   END LOOP;

   DBMS_OUTPUT.PUT_LINE('MATRIX1:a');      
   FOR i in 1 ..v_n LOOP
       DBMS_OUTPUT.PUT('        ..................    ');
       FOR j in 1 ..v_m LOOP
           idx:=(j-1)*v_n + i;
           DBMS_OUTPUT.PUT(' |'||j||','||i||'a['||idx||']'|| rpad(mat1(idx), 4));               
       END LOOP;
       DBMS_OUTPUT.PUT_LINE('');
    END LOOP; 

   DBMS_OUTPUT.PUT_LINE('MATRIX2:b');      
   FOR i in 1 ..v_m LOOP
       FOR j in 1 ..v_n LOOP
           idx:=(j-1)*v_m + i;
           DBMS_OUTPUT.PUT(' |'||j||','||i||'b['||idx||']'|| rpad(mat2(idx), 4));               
       END LOOP;
       DBMS_OUTPUT.PUT_LINE('');
   END LOOP; 

   DBMS_OUTPUT.PUT_LINE('product: a x b');      
   FOR L in 1 ..v_m LOOP
       DBMS_OUTPUT.PUT(' ---------------------------');
       FOR K in 1 ..v_m LOOP
           v_p:=0;
           DBMS_OUTPUT.PUT(' | ');
           FOR j in 1 ..v_n LOOP
               idx1 := j + (K-1)*v_n;
               idx2 := (j-1)*v_m + L;
               v_p := v_p + mat1(idx1) *  mat2(idx2) ;
               DBMS_OUTPUT.PUT('a['||idx1||']b['||idx2||']+');
           END LOOP;
           DBMS_OUTPUT.PUT('->'|| rpad(v_p, 4));               
       END LOOP;
       DBMS_OUTPUT.PUT_LINE('');
    END LOOP; 

END;
/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I set a specific set of numbers in an array/matrix to print?

From Dev

How can I access a multi-dimensional associative array to print a table?

From Dev

How can I create an associative array from a foreach loop?

From Dev

How can I get the prev/next key from an associative array?

From Dev

How can I change the named keys in an associative array

From Dev

How can I make dynamic associative array with jquery?

From Dev

How can I print the lowest key of an array?

From Dev

How can I print a multidimensional array in Perl?

From Dev

How can I print array in Assembly

From Dev

How can I print the lowest key of an array?

From Dev

how I can print an array as a vector form

From Dev

How can I print a x by x matrix in C?

From Dev

How can I print a matrix in R with NA values hidden?

From Dev

How can I print out a matrix in the following format in prolog

From Dev

How can I print a matrix using numpy without ellipsis in the middle?

From Dev

How can I copy associative array items to another single array item recursively?

From Dev

How can I build an associative array recursively using keys from another array?

From Dev

How do I define an associative array?

From Dev

How to get the i'th element of an associative array?

From Dev

How can I save an array to UserDefaults, and then retrieve that array and print

From Dev

How to print associative array data in a Smarty template in following scenario?

From Dev

How can I output specific data from an associative array in PHP with foreach

From Dev

How can I use preg_match_all() to convert a string with 2 delimiters into an associative array?

From Dev

Given an associative array where the values are flat arrays, how can I search the values and return the key?

From Dev

How can I output specific data from an associative array in PHP with foreach

From Dev

How can I use preg_match_all() to convert a string with 2 delimiters into an associative array?

From Dev

How I can show only three elements of my associative array using for?

From Dev

Why I can not unset array in associative array php

From Dev

print partial associative array in php

Related Related

  1. 1

    How can I set a specific set of numbers in an array/matrix to print?

  2. 2

    How can I access a multi-dimensional associative array to print a table?

  3. 3

    How can I create an associative array from a foreach loop?

  4. 4

    How can I get the prev/next key from an associative array?

  5. 5

    How can I change the named keys in an associative array

  6. 6

    How can I make dynamic associative array with jquery?

  7. 7

    How can I print the lowest key of an array?

  8. 8

    How can I print a multidimensional array in Perl?

  9. 9

    How can I print array in Assembly

  10. 10

    How can I print the lowest key of an array?

  11. 11

    how I can print an array as a vector form

  12. 12

    How can I print a x by x matrix in C?

  13. 13

    How can I print a matrix in R with NA values hidden?

  14. 14

    How can I print out a matrix in the following format in prolog

  15. 15

    How can I print a matrix using numpy without ellipsis in the middle?

  16. 16

    How can I copy associative array items to another single array item recursively?

  17. 17

    How can I build an associative array recursively using keys from another array?

  18. 18

    How do I define an associative array?

  19. 19

    How to get the i'th element of an associative array?

  20. 20

    How can I save an array to UserDefaults, and then retrieve that array and print

  21. 21

    How to print associative array data in a Smarty template in following scenario?

  22. 22

    How can I output specific data from an associative array in PHP with foreach

  23. 23

    How can I use preg_match_all() to convert a string with 2 delimiters into an associative array?

  24. 24

    Given an associative array where the values are flat arrays, how can I search the values and return the key?

  25. 25

    How can I output specific data from an associative array in PHP with foreach

  26. 26

    How can I use preg_match_all() to convert a string with 2 delimiters into an associative array?

  27. 27

    How I can show only three elements of my associative array using for?

  28. 28

    Why I can not unset array in associative array php

  29. 29

    print partial associative array in php

HotTag

Archive