Accessing column elements in a 2D array

Hi,

I am trying to access column elements in a 2D array.
I tried using “foreach” construct but it does not work.

My goal is to traverse the column and see if all bits are 1 or all bits are 0.

This is the logic.

//column traverse

 int matrix[3][3] = '{'{1,0,1},'{0,1,1},'{0,0,1}};
    
 int res_1[$],res_0[$];

foreach(matrix[,j]) begin
      res_1 =  matrix[j].find(x) with (x == 1); 
        if (res_1.size == 3)
           return 1;
       
      res_0 =  matrix[j].find(y) with (y == 0); 
        if (res_0.size == 3)
           return 0;
        
end


//row traverse is working fine

    foreach(matrix[i]) begin
       res_1 =  matrix[i].find(x) with (x == 1); 
        if (res_1.size == 3)
           return 1;
       
        res_0 =  matrix[i].find(y) with (y == 0); 
        if (res_0.size == 3)
           return 0;
        
      end

In reply to manjutumn:

Please use code tags making your code easier to read. I have added them for you.

Did you mean to declare a 2D array of bit and not int? And you probably want to return something other than 0 or 1 if neither condition is met.

Within the body of the foreach loop, it has no idea what I and k are iterating over. matrix[k] is still a select of the first dimension. You might want to use the array reductions operators.

foreach(matrix[,k]) begin
      if(matrix.and(col) with (matrix[col.index][k] == 1)) return 1;
      if(matrix.and(col) with (matrix[col.index][k] == 0)) return 0;
end
return -1;

foreach(matrix[i]) begin
        if (matrix[i].and(row) with (row == 1)) return 1;
        if (matrix[i].and(row) with (row == 0)) return 0;
end
return -1;

In reply to dave_59:

Thanks Dave.
I was referring to 2D array of bits.

In reply to manjutumn:

For what it’s worth - I’ve ended up writing very low level (parameterized) Matrix transpose functions and modules to ease these sorts of operations.

i.e.

bit [ ROWS - 1 : 0 ][ COLS - 1 : 0 ][ WORDSIZE - 1 : 0] matrix_a;
bit [ COLS - 1 : 0 ][ ROWS - 1 : 0 ][ WORDSIZE - 1 : 0 ] matrix_a_transposed;
assign matrix_a_transposed = matrix_transpose( matrix_a );

(Actual contents of “matrix_transpose” left out for brevity - as I said it could be a function or even a module instance.)
I’m wondering if there’s better ways to do these sorts of things in the language…

In reply to dave_59:

Hi Dave,

Could you please explain what below lines do? I checked the array reduction methods but I am having hard time to understand array reduction methods when used with ‘with’ clause. Also col.index?

matrix.and(col) with (matrix[col.index][k] == 1)
matrix.and(col) with (matrix[col.index][k] == 0)

I tried to run above code but could not make sense of the output.

In reply to kuki2002:


bit arr[5] = {1,0,1,0,1};

arr.and() // and operation of all the element of arr ,  ans = 1 & 0 & 1 & 0 & 1
arr.and(x) with (x) //This is same as above, here x represent the element , ans  = 1 & 0 & 1 & 0 & 1
arr.and(x) with (x.index) //and operation of all the index, x represent the element , x.index represent the index of that element , ans = 0 & 1 & 2 & 3 & 4
arr.and(x) with (x.index > 2) // and operation of all the index higher than 2, ans = 3 & 4


bit matrix[3][3] = '{'{1,0,1},'{0,1,1},'{0,0,1}};

matrix.and(col) with (matrix[col.index][k] == 1)   

matrix.and(col) // Here col is 1st dimension values of matrix  
// col = {1,0,1} when matrix[0] -> here col.index = 0
// col = {0,1,1} when matrix[1] -> here col.index = 1
// col = {0,0,1} when matrix[2] -> here col.index = 2 

matrix[col.index][k] //lets assume k = 0;
//matrix[col.index][0] -> col.index= 0 -> matrix[0][0] = 1
//matrix[col.index][0] -> col.index= 1 -> matrix[1][0] = 0
//matrix[col.index][0] -> col.index= 2 -> matrix[2][0] = 0

(matrix[col.index][k] == 1)  
// {1==1, 0==1, 0==1} 

matrix.and(col) with (matrix[col.index][k] == 1) 
// and of {1==1, 0==1, 0==1}
// and of {1, 0, 0} which is 0



https://www.linkedin.com/in/patel-rahulkumar/

In reply to Rahulkumar:

thanks for the detailed explanation.