【问题标题】:Removing row and column of matrix after looking for a value (C)查找值后删除矩阵的行和列 (C)
【发布时间】:2013-05-07 09:19:47
【问题描述】:

几个小时前我创建了a question,但在完成我在该问题上提出的问题后,我对自己必须做的事情感到一团糟。人们给我的所有解决方案都还可以,但对于我真正想要的东西没有用,因为我没有写出必须的问题。我必须保存一个值的重要位置,没有必要保存在另一个问题上来解决问题。所以这是正确的。

(上面的例子解释了一切,理解起来很容易)我有一个8x8矩阵,在选择我想要的行之后,我想得到它的三个最小元素,并随机选择这三个中的一个.然后,删除包含此数字的行和列。问题是我不知道如何处理这三个元素并删除列/行。我只知道如何获取最小元素,就是下面的代码。

int pieza[ROWS][COLS] = {
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
0, 1, 3, 2, 0, 0, 0, 0,
};

int myrow = 3; // the row I want to analyze
int index;
int min=0;

for (index=0;index<8;index++) {
    printf("%d", piezas[myrow][index] );
    if(piezas[myrow][index]<min)
        min=piezas[myrow][index];
    printf("\t\t");
}
printf("min: %d", min);

这就是我想做的。如果初始矩阵是(始终是nxn 矩阵):

{
0, 2, 2, 5, 3, 2, 1, 1,
0, 4, 5, 2, 4, 3, 0, 0,
0, 4, 2, 2, 1, 2, 3, 2,
0, 3, 1, 5, 1, 2, 3, 4,
2, 5, 6, 5, 3, 1, 2, 7,
8, 2, 0, 0, 0, 2, 1, 1,
1, 2, 2, 1, 1, 6, 3, 4,
0, 1, 3, 2, 0, 0, 0, 0,
};

我选择第 3 行:

0, 3, 1, 5, 1, 2, 3, 4,

算法必须选择该行的三个最小元素。

0, 1, 1

然后从这三个中随机选择一个。例如,如果它选择第一个“一个”...

0, **1**, 1

...算法必须到该行的第 3 列(因为那是那个 '1' 的位置)并删除行和列,所以输出矩阵如下,一维小于原始矩阵(因为您删除了一行和一列):

    {
    0, 2, 5, 3, 2, 1, 1,
    0, 4, 2, 4, 3, 0, 0,
    0, 4, 2, 1, 2, 3, 2,
    2, 5, 5, 3, 1, 2, 7,
    8, 2, 0, 0, 2, 1, 1,
    1, 2, 1, 1, 6, 3, 4,
    0, 1, 2, 0, 0, 0, 0,
    };

我只知道如何到达线路,但我在处理三个最小值时遇到了问题,因为我有大量的问题指针,而且我不太了解 C。
提前致谢

【问题讨论】:

  • 你的最终矩阵(期望的输出)没有意义,你不能有一个声明为[8][8] 的矩阵,而只是为一个 7x7 矩阵提供初始化器。您在 7 个元素之后放置换行符这一事实对 C 编译器没有任何意义,它会给您一个 8x8 矩阵,因为这就是您所要求的。
  • 我的错。如果您给出nxn 矩阵,则输出矩阵必须是n-1 x n-1 矩阵。少一维。帖子正在编辑中。

标签: c matrix row


【解决方案1】:

按列数排序的示例。

#include <stdio.h>
#include <stdlib.h>

typedef struct pair {
    int value, column;
} Pair;

int cmp(const void *a, const void *b){
    Pair *pa = (Pair *)a;
    Pair *pb = (Pair *)b;
    return pa->value - pb->value;
}

int main(void){
    int data[8] = {0, 3, 1, 5, 1, 2, 3, 4};
    Pair data_pair[8];
    int i;
    for(i=0;i<8;++i){
        data_pair[i].value = data[i];
        data_pair[i].column = i;
    }
    qsort(data_pair, 8, sizeof(Pair), cmp);
    for(i=0;i<3;++i)
        printf("value = %d, column = %d\n", data_pair[i].value, data_pair[i].column);
    return 0; 
}
/* result
value = 0, column = 0
value = 1, column = 2
value = 1, column = 4
*/

【讨论】:

  • 非常感谢 BLUEPIXY。
【解决方案2】:
#include <stdio.h>
#include <string.h>

#define SIZE 8

void delrow(int a[SIZE][SIZE], int row){
    if(row < SIZE - 1)
        memmove(&a[row], &a[row+1], (SIZE*SIZE - SIZE*(row+1))*sizeof(int));
};
void delcol(int a[SIZE][SIZE], int col){
    int r;
    if(col < SIZE - 1){
        for(r=0;r<SIZE;++r){
            memmove(&a[r][col], &a[r][col+1], (SIZE - (col+1))*sizeof(int));
        }
    }
}

int main(void){
    int piezas[8][8] = {
        0, 2, 2, 5, 3, 2, 1, 1,
        0, 4, 5, 2, 4, 3, 0, 0,
        0, 4, 2, 2, 1, 2, 3, 2,
        0, 3, 1, 5, 1, 2, 3, 4,
        2, 5, 6, 5, 3, 1, 2, 7,
        8, 2, 0, 0, 0, 2, 1, 1,
        1, 2, 2, 1, 1, 6, 3, 4,
        0, 1, 3, 2, 0, 0, 0, 0,
    };
    //test
    int row = 8, col = 8;
    int r,c;
    delrow(piezas, 3);
    row -= 1;
    for(r=0;r<row;++r){
        for(c=0;c<col;++c)
            printf("%2d", piezas[r][c]);
        printf("\n");
    }
    printf("\n");
    delcol(piezas, 1);
    col -= 1;
    for(r=0;r<row;++r){
        for(c=0;c<col;++c)
            printf("%2d", piezas[r][c]);
        printf("\n");
    }
    return 0;
}
/* result
 0 2 2 5 3 2 1 1
 0 4 5 2 4 3 0 0
 0 4 2 2 1 2 3 2
 2 5 6 5 3 1 2 7
 8 2 0 0 0 2 1 1
 1 2 2 1 1 6 3 4
 0 1 3 2 0 0 0 0

 0 2 5 3 2 1 1
 0 5 2 4 3 0 0
 0 2 2 1 2 3 2
 2 6 5 3 1 2 7
 8 0 0 0 2 1 1
 1 2 1 1 6 3 4
 0 3 2 0 0 0 0
*/

【讨论】:

  • 感谢您的回答,但这只会删除一列而不是一行。问题是,在到达删除行和列之前,我无法找到您在帖子中看到的值。
  • @Borja 您的数据似乎已更改,但我认为我的程序没有问题。您的示例删除第二列。
  • 我只是把矩阵放在nxnformat 上。你的输入矩阵有 7 行,你的输出矩阵也有 7 行,输出矩阵必须少一行,就像列一样,因为你必须删除两者,而你只删除一列。检查帖子的示例。提前致谢。
  • @Borja 我已经解决了,输入是你的事。
  • 知道了,现在可以使用了。在删除行和列之前,你能给我一些关于如何做所有我必须做的事情的想法吗?我需要帮助来获得三个最小值并随机选择一个。谢谢。
【解决方案3】:

以下是从选定行中获取第 n 个最小元素的提取 - 您在删除行和列之前的第一个要求。

  1. 复制行
  2. 对复制的行进行排序(排序时也保存索引)
  3. 排序后的索引表示排序后值的位置
  4. 选择第 0 分钟或第 1 分钟或第 n 分钟索引排序的索引。

------------非常草稿代码----尝试优化--------

#include <stdio.h>
#include<memory.h>

void sortIndex(int *array, int *arrayIdx)
{
 int i=0,j=0;
 int temp=0;

 int tempArr[4];

 memcpy(tempArr, array, 4*sizeof(int));
 for(i=0;i<4;i++)
 {
  printf("%d ",tempArr[i]);
 }
 printf("\n");

  for(i=0;i<4;i++)
  {
    for(j=i+1;j<4;j++)
    {
      if(tempArr[i]>tempArr[j])
      {
       temp = arrayIdx[i];
       arrayIdx[i]=arrayIdx[j];
       arrayIdx[j]=temp;

       temp = tempArr[i];
       tempArr[i]=tempArr[j];
       tempArr[j]=temp;
      }
    }
  }
 printf("Sorted array Index\n");
 for(i=0;i<4;i++)
 {
  printf("%d ",arrayIdx[i]);
 }
 printf("\n");
 printf("Sorted array Value\n");
 for(i=0;i<4;i++)
 {
  printf("%d ",array[arrayIdx[i]]);
 }
 printf("\n");
}


int main ()
{
 int array[4][4] = {{4,3,2,1},{7,5,4,3},{6,5,4,4},{5,5,2,1}};
 int sortedIdx[4] = {0,1,2,3};
 int i,ii;

 for(i=0;i<4;i++)
 {
   for(ii=0;ii<4;ii++)
      printf("%d ",array[i][ii]);
   printf("\n");
 }

 printf("(Note:Count from 0). Which Row : ");
 scanf("%d",&i);
 sortIndex(array[i],sortedIdx);

 printf("\n");

 printf("(Nth smallest value)Give a N value (0 to 3): ");
 scanf("%d",&ii);
 printf(" (%d)  smallest value in row (%d) is (%d)\n",ii,i,array[i][sortedIdx[ii]]);

 printf("Now call function to remove Row (%d) and column (%d)\n",i,sortedIdx[ii]);

 return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-17
    • 2019-07-08
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 2016-05-26
    相关资源
    最近更新 更多