[問題] 急! MXN矩陣 作轉置使用c或c++
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
c or c++
問題(Question):
example:
把5x5矩陣使用快速轉置矩陣演算法做轉換
餵入的資料(Input):
1 <--- 矩陣個數
5 <--- row個數
5 <--- col個數
0 0 4 0 1
0 1 0 3 0
0 0 0 0 2
2 3 0 1 0
5 0 4 0 0
預期的正確結果(Expected Output):
Sparse matrix:
row col value
0 5 5 10
1 0 3 4
2 0 4 1
3 1 1 1
4 1 3 3
5 2 4 2
6 3 0 2
7 3 1 3
8 3 3 1
9 4 0 5
10 4 2 4
--------------------------------
0 1 2 3 4
RowTerms 2 2 1 3 2
startingPos 1 3 5 6 9
-------------------------------
Transpose matrix:
row col value
0 5 5 10
1 0 3 2
2 0 4 5
3 1 1 1
4 1 3 3
5 2 4 4
6 3 0 4
7 3 1 3
8 3 3 1
9 4 0 1
10 4 2 2
----------------------------
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
快速轉置矩陣程式碼
void fast_transpose(term a[ ], term b[ ])
{
int row_terms[MAX_COL], starting_pos[MAX_COL];
int i, j, num_cols = a[0].col, num_terms = a[0].value;
b[0].row = num_cols; b[0].col = a[0].row;
b[0].value = num_terms;
if (num_terms > 0)
{
for (i = 0; i < num_cols; i++)
row_terms[i] = 0;
for (i = 1; i <= num_terms; i++)
row_term [a[i].col]++;
starting_pos[0] = 1;
for (i =1; i < num_cols; i++)
starting_pos[i]=starting_pos[i-1] +row_terms [i-1];
// cout << row_terms [i-1];
// cout << starting_pos[i]];
for (i=1; i <= num_terms, i++)
{
j = starting_pos[a[i].col];
b[j].row = a[i].col;
b[j].col = a[i].row;
b[j].value = a[i].value;
startingPos[a[i].col]++;
// cout << a[i].col << a[i].row << a[i].value;
}
}
}
補充說明(Supplement):
由於是大二作業,不得不交,已請教多位高手
仍無法獲得解決,懇請版上高手幫忙
現在方向要先定義資料結構,在讀5X5矩陣作轉換,題目改成只有5x5,就單純範例去跑就好
感覺難度有降低一點,不過卡在讀檔QQ
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 36.237.163.36
※ 編輯: artorius 來自: 36.237.163.36 (10/21 00:09)
→
10/21 01:39, , 1F
10/21 01:39, 1F
推
10/21 01:40, , 2F
10/21 01:40, 2F
推
10/21 02:16, , 3F
10/21 02:16, 3F
→
10/21 03:10, , 4F
10/21 03:10, 4F
→
10/21 03:12, , 5F
10/21 03:12, 5F
→
10/21 03:13, , 6F
10/21 03:13, 6F
推
10/21 09:30, , 7F
10/21 09:30, 7F
→
10/21 10:47, , 8F
10/21 10:47, 8F
→
10/21 10:47, , 9F
10/21 10:47, 9F
→
10/21 10:57, , 10F
10/21 10:57, 10F
推
10/21 16:35, , 11F
10/21 16:35, 11F
→
10/21 16:36, , 12F
10/21 16:36, 12F
※ 編輯: artorius 來自: 36.237.163.36 (10/21 16:51)
→
10/21 22:24, , 13F
10/21 22:24, 13F
→
10/22 20:22, , 14F
10/22 20:22, 14F