Sorting rows of a sparse CSC matrix Golang

huangapple go评论88阅读模式
英文:

Sorting rows of a sparse CSC matrix Golang

问题

我正在尝试分析稀疏矩阵。面临的任务是按照原始矩阵中各行的元素升序对行进行排序。

但是我不明白如何在不破坏空元素的情况下完成这个任务。

我尝试将和数组的元素与行绑定并进行移动。但是一些元素已经从CSC结构中删除了。

可能需要修改li/lj数组本身,但是我对此缺乏足够的数学知识。更确切地说,我不明白如何在没有在结构中明确指定附加元素(零)的情况下跟踪何时应该重新排列元素。

以下是翻译好的代码部分:

package main

import (
	"fmt"
)

type CSC struct {
	a, lj, li []int
}

func getEl(i, j int, el *CSC) int {
	for k := el.lj[j]; k < el.lj[j+1]; k++ {
		if el.li[k] == i {
			return el.a[k]
		}
	}
	return 0
}

func maxSliceEl(lj []int) int {
	max := 0

	for _, v := range lj {
		if v > max {
			max = v
		}
	}
	return max
}

func main() {
	ma := CSC{
		a:  []int{8, 2, 5, 7, 1, 9, 2},
		li: []int{0, 0, 1, 4, 4, 6, 4},
		lj: []int{0, 1, 1, 4, 6, 7},
	}

	n := len(ma.lj) + 1
	m := maxSliceEl(ma.li) - 1
	fmt.Printf("列数: %v, 行数: %v\n", n, m)

	maxStr := []int{}

	fmt.Println("初始矩阵:")
	for i := 0; i < n; i++ {

		sumStrEl := 0
		for j := 0; j < m; j++ {
			fmt.Print(getEl(i, j, &ma), " ")
			sumStrEl += getEl(i, j, &ma)
		}

		maxStr = append(maxStr, sumStrEl)
		fmt.Println("|sumStrEl: ", sumStrEl)
	}

}

希望对你有帮助!

英文:

I'm trying to analyze sparse matrices. Faced with the task of sorting rows in ascending order of the elements in them in the original matrix.

But I don't understand how to do this without damaging the empty elements.

I tried to bind the elements of the sum array to the rows and somehow move them. But some elements have been removed from the CSC structure.

It may be necessary to change the li/lj arrays themselves, but I don't have enough mathematical knowledge for this. More precisely, I don't understand how to track when elements should be rearranged unless additional elements (zeros) are explicitly specified in the structure.

package main
import (
&quot;fmt&quot;
)
type CSC struct {
a, lj, li []int
}
func getEl(i, j int, el *CSC) int {
for k := el.lj[j]; k &lt; el.lj[j+1]; k++ {
if el.li[k] == i {
return el.a[k]
}
}
return 0
}
func maxSliceEl(lj []int) int {
max := 0
for _, v := range lj {
if v &gt; max {
max = v
}
}
return max
}
func main() {
ma := CSC{
a:  []int{8, 2, 5, 7, 1, 9, 2},
li: []int{0, 0, 1, 4, 4, 6, 4},
lj: []int{0, 1, 1, 4, 6, 7},
}
n := len(ma.lj) + 1
m := maxSliceEl(ma.li) - 1
fmt.Printf(&quot;Col: %v, Row: %v\n&quot;, n, m)
maxStr := []int{}
fmt.Println(&quot;Initial matrix:&quot;)
for i := 0; i &lt; n; i++ {
sumStrEl := 0
for j := 0; j &lt; m; j++ {
fmt.Print(getEl(i, j, &amp;ma), &quot; &quot;)
sumStrEl += getEl(i, j, &amp;ma)
}
maxStr = append(maxStr, sumStrEl)
fmt.Println(&quot;|sumStrEl: &quot;, sumStrEl)
}
}

答案1

得分: 0

我通过将结构作为解决方案来找到了问题的解决方法:元素的总和+它们的索引。解决方案比预期的简单,只是缺乏解决稀疏矩阵的实践。总和的位置[i]必须作为第一个参数传递给getEl函数。

package main

import (
	"fmt"
	"sort"
)

// 创建CSC(CCS)矩阵结构
type CSC struct {
	// 值数组,列索引,行索引
	a, lj, li []int
}

// 获取元素
func getEl(i, j int, el *CSC) int {
	for k := el.lj[j]; k < el.lj[j+1]; k++ {
		// 如果元素的行等于搜索元素的行,则找到该元素
		if el.li[k] == i {
			return el.a[k]
		}
	}
	// 否则,返回0。它将被输入到矩阵中
	return 0
}

func maxSliceEl(lj []int) int {
	max := 0

	for _, v := range lj {
		if v > max {
			max = v
		}
	}
	return max
}

type strInfo struct {
	summa int
	pos   int
}

func main() {
	// 设置CSC矩阵
	ma := CSC{
		a:  []int{8, 2, 5, 7, 1, 9, 2},
		li: []int{0, 0, 1, 4, 4, 6, 4},
		lj: []int{0, 1, 1, 4, 6, 7},
	}

	// 定义列数
	n := len(ma.lj) + 1
	// 定义行数
	m := maxSliceEl(ma.li) - 1
	fmt.Printf("Cols: %v, Rows: %v\n", m, n)

	// 为计算每行的总和和索引每个元素而设置具有结构类型的变量
	var stringsInfo []strInfo

	fmt.Println("初始矩阵:")
	for i := 0; i < n; i++ {
		sumStrEl := 0

		for j := 0; j < m; j++ {
			sumStrEl += getEl(i, j, &ma)
			fmt.Print(getEl(i, j, &ma), " ")
		}
		fmt.Println("|", sumStrEl)

		// 将具有总和和索引的单元格添加到切片中
		var strI strInfo
		strI.summa = sumStrEl
		strI.pos = i
		stringsInfo = append(stringsInfo, strI)
	}

	fmt.Println("stringsInfo: ", stringsInfo)

	// 按元素总和的升序对stringsInfo切片进行排序
	sort.Slice(stringsInfo, func(i, j int) (less bool) {
		return stringsInfo[i].summa < stringsInfo[j].summa
	})

	fmt.Println("stringsInfo: ", stringsInfo)

	fmt.Println("排序后的矩阵:")
	for i := range stringsInfo {
		for j := 0; j < m; j++ {
			// 按索引stringsInfo[i].pos输出矩阵
			fmt.Print(getEl(stringsInfo[i].pos, j, &ma), " ")
		}
		fmt.Println("|", stringsInfo[i].summa)
	}
}
英文:

I found a solution to the problem by taking the structure as a solution: the sum of the elements + their index. The solution turned out to be simpler than expected, only the practice of solving sparse matrices was lacking. The position [i] of the sum must be passed to the getEl function as the first parameter.

package main
import (
&quot;fmt&quot;
&quot;sort&quot;
)
// Creating a CSC (CCS) matrix structure
type CSC struct {
// Array of values, column indexes, row indexing
a, lj, li []int
}
// Getting access to the element
func getEl(i, j int, el *CSC) int {
for k := el.lj[j]; k &lt; el.lj[j+1]; k++ {
// If the element string is equal to the string of the searched element, then the element is found
if el.li[k] == i {
return el.a[k]
}
}
// Otherwise, we will return 0. It will be entered into the matrix
return 0
}
func maxSliceEl(lj []int) int {
max := 0
for _, v := range lj {
if v &gt; max {
max = v
}
}
return max
}
type strInfo struct {
summa int
pos   int
}
func main() {
// Set the CSC matrix
ma := CSC{
a:  []int{8, 2, 5, 7, 1, 9, 2},
li: []int{0, 0, 1, 4, 4, 6, 4},
lj: []int{0, 1, 1, 4, 6, 7},
}
// Define the number of columns
n := len(ma.lj) + 1
// Define the number of rows
m := maxSliceEl(ma.li) - 1
fmt.Printf(&quot;Cols: %v, Rows: %v\n&quot;, m, n)
// Set a variable with a structure type for calculating 
// the amount in a row and indexing each element in it
var stringsInfo []strInfo
fmt.Println(&quot;Initial matrix:&quot;)
for i := 0; i &lt; n; i++ {
sumStrEl := 0
for j := 0; j &lt; m; j++ {
sumStrEl += getEl(i, j, &amp;ma)
fmt.Print(getEl(i, j, &amp;ma), &quot; &quot;)
}
fmt.Println(&quot;|&quot;, sumStrEl)
// Adding a cell with the sum and index to the slice
var strI strInfo
strI.summa = sumStrEl
strI.pos = i
stringsInfo = append(stringsInfo, strI)
}
fmt.Println(&quot;stringsInfo: &quot;, stringsInfo)
// Sorting the stringsInfo slice in ascending order of the sum elements
sort.Slice(stringsInfo, func(i, j int) (less bool) {
return stringsInfo[i].summa &lt; stringsInfo[j].summa
})
fmt.Println(&quot;stringsInfo: &quot;, stringsInfo)
fmt.Println(&quot;Sorted matrix:&quot;)
for i := range stringsInfo {
for j := 0; j &lt; m; j++ {
// Output the matrix by idnex stringsInfo[i].pos
fmt.Print(getEl(stringsInfo[i].pos, j, &amp;ma), &quot; &quot;)
}
fmt.Println(&quot;|&quot;, stringsInfo[i].summa)
}
}

huangapple
  • 本文由 发表于 2022年10月22日 05:59:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/74159542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定