https://stackoverflow.com/questions/17447817/correlation-of-two-arrays-in-c-sharp

 

public double ComputeCoeff(double[] values1, double[] values2)
{
    if(values1.Length != values2.Length)
        throw new ArgumentException("values must be the same length");

    var avg1 = values1.Average();
    var avg2 = values2.Average();

    var sum1 = values1.Zip(values2, (x1, y1) => (x1 - avg1) * (y1 - avg2)).Sum();

    var sumSqr1 = values1.Sum(x => Math.Pow((x - avg1), 2.0));
    var sumSqr2 = values2.Sum(y => Math.Pow((y - avg2), 2.0));

    var result = sum1 / Math.Sqrt(sumSqr1 * sumSqr2);

    return result;
}

예)

var values1 = new List<double> { 3, 2, 4, 5 ,6 };
var values2 = new List<double> { 9, 7, 12 ,15, 17 };

var result = ComputeCoeff(values1.ToArray(), values2.ToArray());
// 0.997054485501581

Debug.Assert(result.ToString("F6") == "0.997054");

Excel 함수 사용 예)

var values1 = new List<double> { 3, 2, 4, 5 ,6 };
var values2 = new List<double> { 9, 7, 12 ,15, 17 };

// Make sure to add a reference to Microsoft.Office.Interop.Excel.dll
// and use the namespace

var application = new Application();

var worksheetFunction = application.WorksheetFunction;

var result = worksheetFunction.Correl(values1.ToArray(), values2.ToArray());

Console.Write(result); // 0.997054485501581

 

https://be2u.tistory.com/144

 

[c#] 상관계수 함수 - Correlation Coefficient Function

상관관계에 대한 공식 정리 c# 기반 소스 코드 private float Correlation(List xList, List yList) { float result = 0; float multiplyXYSigma = 0; float xSigma = 0; float ySigma = 0; float x..

be2u.tistory.com

 

'Programming Language > C#' 카테고리의 다른 글

DataTable 데이터 복제  (0) 2020.06.03
NLog 사용 샘플  (0) 2020.04.25
ChartFX Help  (0) 2020.01.19
remove comments with C#  (0) 2019.10.14
excel upload faster  (0) 2019.10.14

중복라인삭제.

- notepad++ 에서 적용

- 조건은 아래 정규식으로, 바꿀내용은 빈공백으로.

^(.*?)$\s+?^(?=.*^\1$)

 

end.

 

1. Chart FX 8

- main link: https://support.softwarefx.com/Chart_FX_8  

- API Reference: http://support.softwarefx.com/Chart_FX_8/api

- Programmer's Guide & Samples: http://support.softwarefx.com/Chart_FX_8/article/2501002

- Samples: http://support.softwarefx.com/Chart_FX_8/samples

- 한국어 퀵가이드: http://download.softwarefx.com/CfxNet80/Chart_FX_8_Quick_Guide_Korean.pdf

 

2. Chart FX 7

- main link: https://support.softwarefx.com/Chart_FX_7/

- API Reference: https://support.softwarefx.com/Chart_FX_7/api

- Programmer's Guide & Samples: https://support.softwarefx.com/Chart_FX_7/article/2501002

- Samples: https://support.softwarefx.com/Chart_FX_7/samples

 

end.

 

'Programming Language > C#' 카테고리의 다른 글

NLog 사용 샘플  (0) 2020.04.25
[펌] Correlation of two arrays in C#  (0) 2020.02.06
remove comments with C#  (0) 2019.10.14
excel upload faster  (0) 2019.10.14
c# 관련사이트  (0) 2019.10.14

 

# remove comments with C#

  1. Remove comments
  2. Maintain line number
using System;
using System.Text;
using System.Windows.Forms;

namespace WFRemoveComment
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnRemoveComment_Click(object sender, EventArgs e)
        {
            string s = System.IO.File.ReadAllText(@"C:\workspace_cs\WindowsFormsApp1\WindowsFormsApp1\Form1.cs");
        
            this.rtxText1.Text = s;
            this.rtxText2.Text = removeComment(s);
        }

        public static string removeCommentStyleJava(string s)
        {
            char lineCommentChar1 = '/';
            char lineCommentChar2 = '/';

            return removeComment(s, lineCommentChar1, lineCommentChar2);
        }

        public static string removeCommentStyleSql(string s)
        {
            char lineCommentChar1 = '-';
            char lineCommentChar2 = '-';

            return removeComment(s, lineCommentChar1, lineCommentChar2);
        }

        public static string removeComment(string s, char lineCommentChar1 = '/', char lineCommentChar2 = '/')
        {
            StringBuilder endResult = new StringBuilder(1000);

            const int outsideComment = 0;
            const int insideLineComment = 1;
            const int insideblockComment = 2;
            
            int currentState = outsideComment;
            if(s == null)
            {
                return endResult.ToString();
            }
            
            char c = (char) 0;
            char c2 = (char)0;
            for(int i = 0, cnt = s.Length; i < cnt; i++)
            {
                c = s[i];
                switch (currentState)
                {
                    case outsideComment:
                        if (c == lineCommentChar1 && (i+1) < cnt)
                        {
                            c2 = s[++i]; // 다음건 read
                            if (c2 == lineCommentChar2)
                                currentState = insideLineComment;
                            else if (c2 == '*')
                                currentState = insideblockComment;
                            else
                                endResult.Append(c).Append(c2);
                        }
                        else
                            endResult.Append(c);
                        break;
                    case insideLineComment:
                        if (c == '\r')
                            endResult.Append(c);

                        if (c == '\n')
                        {
                            currentState = outsideComment;
                            endResult.Append(c);
                        }
                        break;
                    case insideblockComment:
                        if (c == '\r' || c == '\n')
                            endResult.Append(c);

                        if (c == '*' && (i+1) < cnt)
                        {
                            c2 = s[++i]; // 다음건 read
                            if (c2 == '/')
                            {
                                currentState = outsideComment;
                                break;
                            }
                        }
                        break;
                } // end of switch
            } // end of for

            return endResult.ToString();
        }
    }
}

 

 

end.

'Programming Language > C#' 카테고리의 다른 글

[펌] Correlation of two arrays in C#  (0) 2020.02.06
ChartFX Help  (0) 2020.01.19
excel upload faster  (0) 2019.10.14
c# 관련사이트  (0) 2019.10.14
Highlight Textbox  (0) 2019.10.14

+ Recent posts