ssa
1package oss2
3// Этот модуль необходим для сохранения данных в папку File_For_MatLab. А файл plotting.m рисует графики на основании полученных данных.
4
5import (6"fmt"7"log"8"math"9"os"10"sort"11"strconv"12
13"github.com/xuri/excelize/v2"14"gonum.org/v1/gonum/mat"15)
16
17// Сохранить данные массива float64
18//
19// xlsx
20func Matlab_arr_float(arr []float64, Path, FileName string) error {21// err := Matlab_mkDir(number, Path)22// if err != nil {23// fmt.Println(err)24// }25file_graph := excelize.NewFile()26file_graph.NewSheet("main")27file_graph.DeleteSheet("Sheet1")28for ind, val := range arr {29file_graph.SetCellValue("main", "A"+strconv.Itoa(ind+1), val)30}31// "File_For_MatLab" + OpSystemFilder + strconv.Itoa(number) + OpSystemFilder + fileName + ".xlsx"32if err := file_graph.SaveAs(Path + FileName); err != nil {33fmt.Println(err)34}35file_graph.Close()36return nil37}
38
39// Сохранить данные массива float64
40//
41// xlsx
42func Matlab_mat_Vector(vect mat.Vector, Path, FileName string) error {43// err := Matlab_mkDir(number, Path)44// if err != nil {45// fmt.Println(err)46// }47file_graph := excelize.NewFile()48file_graph.NewSheet("main")49file_graph.DeleteSheet("Sheet1")50for ind := 0; ind < vect.Len(); ind++ {51file_graph.SetCellValue("main", "A"+strconv.Itoa(ind+1), vect.AtVec(ind))52}53// "File_For_MatLab" + OpSystemFilder + strconv.Itoa(number) + OpSystemFilder + fileName + ".xlsx"54if err := file_graph.SaveAs(Path + FileName); err != nil {55fmt.Println(err)56}57file_graph.Close()58return nil59}
60
61// Сохранить данные переменной int
62//
63// txt
64func Matlab_variable(data int, Path, FileName string) error {65// err := Matlab_mkDir(number, Path)66// if err != nil {67// fmt.Println(err)68// }69// create file70// Path + "File_For_MatLab" + OpSystemFilder + strconv.Itoa(number) + OpSystemFilder + fileName + ".txt"71f, err := os.Create(Path + FileName)72if err != nil {73log.Println(err)74}75// remember to close the file76defer f.Close()77
78_, err = f.WriteString(fmt.Sprintf("%v", data))79
80return err81}
82
83func Mean(arr []float64) float64 {84var meanVar float6485for _, val := range arr {86meanVar += val87}88return meanVar / float64(len(arr))89}
90
91// *****************************************************************************
92
93func Prctile(input []float64, percent float64) float64 {94var percentile float6495length := len(input)96if length == 0 {97return math.NaN()98}99
100if length == 1 {101return input[0]102}103
104if percent <= 0 || percent > 100 {105return math.NaN()106}107
108// Start by sorting a copy of the slice109//c := sortedCopy(input)110sort.Float64s(input)111
112// Multiply percent by length of input113index := (percent / 100) * float64(len(input))114
115// Check if the index is a whole number116if index == float64(int64(index)) {117
118// Convert float to int119i := int(index)120
121// Find the value at the index122percentile = input[i-1]123
124} else if index > 1 {125
126// Convert float to int via truncation127i := int(index)128
129// Find the average of the index and following values130percentile = Mean([]float64{input[i-1], input[i]}) // Mean(Float64Data{input[i-1], input[i]})131
132} else {133return math.NaN()134}135
136return percentile137
138}
139
140// *****************************************************************************
141
142// Сохранить данные матрицы mat.Dense
143func Matlab_mat_Dense(X *mat.Dense, Path, FilePathName string) error {144file_graph := excelize.NewFile()145file_graph.NewSheet("main")146file_graph.DeleteSheet("Sheet1")147n, m := X.Dims()148for i := 0; i < n; i++ {149for j := 0; j < m; j++ {150file_graph.SetCellValue("main", GetColumnName(j+1)+strconv.Itoa(i+1), X.At(i, j))151}152}153// Path + "File_For_MatLab" + OpSystemFilder + strconv.Itoa(number) + OpSystemFilder + fileName + ".xlsx"154if err := file_graph.SaveAs(Path + FilePathName); err != nil {155fmt.Println(err)156}157file_graph.Close()158return nil159}
160