How can we find sum of diagonal matrix using powershell

Narasimha 61 Reputation points
2021-07-19T12:20:40.58+00:00

Sum of diagonal matrix in power shell

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 60,161 Reputation points
    2021-07-19T14:05:38.463+00:00

    PS is basically .NET so any .NET solution will work. For example here's one I googled real quick.

    $sum = 0
    $matrix = @(@(1, 2, 3, 4), @(5, 6, 7, 8), @(1, 2, 3, 4), @(5, 6, 7, 8))
    for ($i = 0; $i -lt $matrix.length; ++$i) { 
       $sum += [double]$matrix[$i][$i]                                                                                     
    }
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2021-07-19T21:40:55.483+00:00

    Do you want the Principal or Secondary sum? Or both?

    Function DiagonalSums{
        [CmdletBinding()]
        param (
            [Parameter()]
            [int[,]]
            $matrix,
    
            [Parameter()]
            [int]
            $n              # order of the matrix
        )    
        [int]$principal = 0
        [int]$secondary = 0
    
        For ($i = 0; $i -lt $n; $i++) {
            $principal += $matrix[$i,$i]
            $secondary += $matrix[$i,($n - $i - 1)]
        }
        [PSCustomObject]@{
            Principal = $principal
            Secondary = $secondary
        }   
    }
    
    # Driver code
    
    #
    # test data
    #
    
    # Principal diagonal = 18, Secondary diagonal = 18
    #$elements = 1,2,3,4,5,6,7,8,1,2,3,4,5,6,7,8
    #$n = 4     # order of the matrix
    
    # Principal diagonal = 16, Secondary diagonal = 20
    #$elements = 1,2,3,4,4,3,2,1,7,8,9,6,6,5,4,3
    #$n=4        # order of the matrix
    
    # Principal diagonal = 3, Secondary diagonal = 3
    $elements = 1,1,1,1,1,1,1,1,1
    $n = 3      # order of the matrix
    
    $mat = New-Object "int[,]" $n,$n
    
    # load the matrix
    $e = 0
    For ($i = 0; $i -lt $n; $i++){
        For ($j = 0; $j -lt $n; $j++){
                $mat[$i,$j] = $elements[$e]
                $e++
        }
    }
    
    DiagonalSums $mat $n
    

  2. Anonymous
    2021-07-20T02:35:05+00:00

    Hi,

    Do you mean the sum of the diagonal elements (the trace) of the diagonal matrix?

    $matrix = @((1,0,0,0),(0,2,0,0),(0,0,3,0),(0,0,0,4))  
    $sum = 0  
    for($i=0;$i -lt $matrix.count;$i++){  
        $sum += $matrix[$i][$i]      
    }  
    $sum  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  3. Narasimha 61 Reputation points
    2021-07-20T05:00:07.487+00:00

    iam trying to give values at runtime but while doing diagonal matrix it throwing error as cannot index into null value array


  4. Rich Matheisen 47,901 Reputation points
    2021-07-20T14:27:04.04+00:00

    If you want the user to enter the data you have only to use the function I gave you to calculate the result but instead of using the method I used (loading from a file), substitute this instead:

    $n = Read-Host "Enter the order of the matrix: "
    $mat = New-Object "int[,]" $n,$n
    Write-Host "Enter the values, one value at a time, row-by-row:"
    For ($i = 0; $i -lt $n; $i++){
        For ($j = 0; $j -lt $n; $j++){
                $mat[$i,$j] = Read-Host "Enter value: "
        }
    }
    

    I'm also curious to know if this is some sort of homework assignment!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.