Processing 180 by 180 array of doubles RRS feed

  • Question

  • What is the optimum way in c sharp to process or parse an 180 by 180 array of double into memory. In a nutshell someone is passing me 180by180 array from Matlab and and I need to parse into memory using c sharp. Do I really need 2-dimensional array to this? Can someone point me in the right direction of process an 180by180 array. This is the code I have below.


    MULTIPLIER = (Math.PI * 2);

    double azmiuthLength = (MULTIPLIER * azmiuth); double elevationLength = (MULTIPLIER * elevation); for (double x = 0; x < Math.PI; x += Math.PI / 180)

    { double xAxis = Math.abs(Math.sin( azmiuthLength * Math.cos(x) / azmiuthLength * Math.cos(x))); this.antennaPattern.getRadiationArray2D()[xIndex][0] = xAxis; int yIndex = 0; for (double y = 0; y < Math.PI; y += Math.PI / 180)

    { double yAxis = Math.abs(Math.sin(elevationLength * Math.cos(y) / elevationLength * Math.cos(y))); antennaPattern.getRadiationArray2D()[xIndex][yIndex] = yAxis; yIndex++; } xIndex++; }


    Saturday, February 9, 2019 10:06 AM

All replies

  • The main problem that I see with your loop is that you are repeatedly calling this method:


    You call it on every iteration, which means that there will be more that thirty thousand calls to the method. Your code does not show what the method does, but if it is just returning the array, it's preferable to call it just once and store a reference to the array in a variable.

    Other that that, your loops appear to be o.k., and they should process the array as expected. Does it produce any error, or otherwise cause you any trouble?

    Saturday, February 9, 2019 12:50 PM
  • Due to computational errors, you probably will observe that xIndex and yIndex become 180 unexpectedly. This will produce IndexOutOfRangeException if the array contains 180 elements only.

    Try a different approach:

    for( int xIndex = 0; xIndex < 180; ++xIndex )


        double x = ( Math.PI * xIndex ) / 180;


        for( int yIndex = 0; yIndex < 180; ++yIndex )


            double y = ( Math.PI * yIndex ) / 180;


            // . . .




    It seems that your yAxis does not depend on x. Therefore the columns will contain the same values.

    Instead of this array, maybe define a function that takes x, y, and other parameters, performs calculations and returns the result.

    • Edited by Viorel_MVP Tuesday, February 19, 2019 9:25 AM
    Saturday, February 9, 2019 2:37 PM
  • Should I use a list collection, instead of 2-dimensional array. 


    Sunday, February 10, 2019 2:57 AM
  • Should I be calling the method one time below?

    this.antennaPattern.getRadiationArray2D()[xIndex][0] = xAxis;


    Sunday, February 10, 2019 2:59 AM
  • Can I just use a dictionary below instead of a 2-d multi array? Is this better. 

    for (double 0(x < Math.PI)(x + (Math.PI / 180))) {
    double xAxis getXAxis(x, azmiuthLength);

    antennaPattern.setAntennaPatternList(new ArrayList<Double>());

    (double 0(y < Math.PI)(y 
                    + (Math.PI / 
    180))) {
    double yAxis getYAxis(y, elevationLength);
        antennaPattern.AntennaPatternDictionary().add(antennaPattern.getAntennaPatternId(), antennaPattern.getAntennaPatternList())


    Sunday, February 10, 2019 5:40 AM
  • Should I be calling the method one time below?

    this.antennaPattern.getRadiationArray2D()[xIndex][0] = xAxis;

    My idea was to call it outside the loop and save it in a variable:

    var theArray = antennaPattern.getRadiationArray2D(); // Sorry, using "var" because I don't know its real type
    for (double x = 0; x < Math.PI; x += Math.PI / 180) 
        theArray[xIndex][0] = xAxis;
           // similarly for the "y" loop

    Of course, this presumes that the getRadiationArray2D method does not have any collateral effects and always returns the same array.

    Sunday, February 10, 2019 7:54 AM
  • , so which do you prefer, dictionary or the 2-dimension array? When I use the 2-dimensional array it took about 10minutes to process. When I use the dictionary, it took seconds to process.

    Am I missing data if I use the dictionary?


    Monday, February 11, 2019 6:22 AM
  • Something is missing in this picture. Normally, accessing an array by index is a very fast operation. It should be much faster that accessing a Dictionary... unless you have to search for data, in which case it is faster to get it from a dictionary key than to loop through the array comparing values.

    But in your case, unless I have misinterpreted your code, you are simply looping through the rows and columns of the array calculating a value for each position. This should always be faster that attempting to do something similar on a Dictionary. If you are getting faster times when using the Dictionary, it means that you are doing some different operations that are not obvious from the code we have seen so far.

    Monday, February 11, 2019 7:20 AM
  • Hi 

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, February 19, 2019 8:13 AM