how to add two rows of matrix??

Posted on 2007-10-20
Last Modified: 2010-04-15

I have this matrix and how can I do this addition of two rows of matrix systematically in c#??

Say I have a matrix of Ma[10,20] and I want to cut this matrix to newMa[5,20]
In order to do that, I want to add two rows and get the average and put it into the newMa.

As example,

2   4   3                                       3     5     5
4    6   7                 to                  3     8     8
3    4   3
3    12  13

Does this make any sense??
Question by:dkim18
    LVL 10

    Assisted Solution

    It makes sense if you have some requirement for this. One question which rows do you want to retain and where should the avg. be replaced in the matrix. if it is the lower rows you want to retain then you need to loop through the array from top to required lower position -1 and add all the number take out the average of the numbers and then replace the result in required lower position.
    LVL 22

    Accepted Solution

    It sounds like you are looking for the midpoints. This would be more interesting and generally useful if your matrix were defined as an array or list of vecoors. However, this will build that matrix for your sample values:

    int[,] matrix = new int[,]{
       {2,4,3}, {4,6,7}, {3,4,3}, {3,12, 13}};
    int[,] midpoints = new int[2, 3];
    int k = 0;
    for (int i = 0; i < 4; i += 2)
       for (int j = 0; j < 3; j++)
          midpoints[k, j] = matrix[i, j] + ((matrix[i + 1, j] - matrix[i, j]) / 2);


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now