鱼C论坛

 找回密码
 立即注册
查看: 4011|回复: 13

如何二维矩阵读入一维矩阵?

[复制链接]
发表于 2019-3-27 11:21:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问一个问题,我在做一个arduino的小项目,想通过步进电机来操作一个机械臂。我想通过记录步进电机的转动坐标来记录机械臂的空间坐标, 以此记录下机械臂的运动路径并将其记录于一个二维数据矩阵中。我的设想是把移动中关键空间坐标记录下来, 比如如果有n步,(x0, y0, z0, 0), (x1,y1,z1,1)... (xn,yn,zn,n)。
我在网上搜了一下,发现这种二维矩阵的写入有两种方式,第一种是一次性写入,比如:
coordinates[5][4] = {{5,7,9,0},{1,3,5,1},{2,4,7,2},{5,8,1,3}}
第二种是逐个元素写入:
int coordintates[5][4]
for(int i=0; i<5; i++){
   for(int j=0; j<4; j++){
    coordinates[j] = values
}
但这两种都不是我想要的方式,我想一次记录一组坐标,像这样:
coordinates[0][4] = {5,7,9,0};
coordinates[1][4] = {1,3,5,1};
coordinates[2][4] = {2,4,7,2};
coordinates[3][4] = {5,8,1,3};
但是发现好像C++不允许这么做,也可能我没有找对正确的方法。
特在这里请教各位同学。谢谢!



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-3-27 11:24:09 | 显示全部楼层
有,直接用内存copy的方法将一个数组拷贝到目标位置即可
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 11:28:19 | 显示全部楼层
BngThea 发表于 2019-3-27 11:24
有,直接用内存copy的方法将一个数组拷贝到目标位置即可

请问具体怎么做, 我是想设置一个interrupt程序coordinate_record(),每触发一次便记录下当前的坐标值(x,y,  z,  n),像这样:
int n = 0;
int coordinates[n][4]
coordinate_record()
{
coordinates[4][n] = {x,y,z,n}
n++
}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 14:12:12 | 显示全部楼层
本帖最后由 Croper 于 2019-3-27 14:23 编辑
老猪想回高老庄 发表于 2019-3-27 11:28
请问具体怎么做, 我是想设置一个interrupt程序coordinate_record(),每触发一次便记录下当前的坐标值( ...


这样写
  1. int coordinates[4][4];
  2. coordinates[0] = {5,7,9,0};
  3. coordinates[1] = {1,3,5,1};
  4. coordinates[2] = {2,4,7,2};
  5. coordinates[3] = {5,8,1,3};
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 14:19:12 | 显示全部楼层
    用结构数组如何:
  1. typedef struct record {
  2.         int x ;
  3.         int y ;
  4.         int z ;
  5.         int n ;
  6. } POINT       ;

  7. void setpoint(POINT * p , const int x , const int y , const int z , const int n)
  8. {
  9.         p -> x = x ;
  10.         p -> y = y ;
  11.         p -> z = z ;
  12.         p -> n = n ;
  13. }

  14. POINT coordinates[4] ;

  15. setpoint(& coordinates[0] , 5 , 7 , 9 , 0) ;
  16. setpoint(& coordinates[1] , 1 , 3 , 5 , 1) ;
  17. setpoint(& coordinates[2] , 2 , 4 , 7 , 2) ;
  18. setpoint(& coordinates[3] , 5 , 8 , 1 , 3) ;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 14:32:11 | 显示全部楼层

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
int coordinates[4][4];
coordinates[0] = {5,7,9,0};
coordinates[1] = {1,3,5,1};
coordinates[2] = {2,4,7,2};
coordinates[3] = {5,8,1,3};

for(int i=0; i < 4; i++){
for(int j =0; j < 4; j++){
  Serial.print(coordinates[i][j]);}
}
}
以上是我的程序, 一直报错:
Arduino: 1.8.5 (Windows 7), Board: "Arduino/Genuino Uno"

C:\Users\Valued Customer\Documents\Arduino\Coordinate\Coordinate.ino: In function 'void setup()':

Coordinate:10: error: assigning to an array from an initializer list

coordinates[0] = {5,7,9,0};

                ^

Coordinate:11: error: assigning to an array from an initializer list

coordinates[1] = {1,3,5,1};

                ^

Coordinate:12: error: assigning to an array from an initializer list

coordinates[2] = {2,4,7,2};

                ^

Coordinate:13: error: assigning to an array from an initializer list

coordinates[3] = {5,8,1,3};

                ^

exit status 1
assigning to an array from an initializer list

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 14:56:56 | 显示全部楼层
本帖最后由 Croper 于 2019-3-27 15:07 编辑
老猪想回高老庄 发表于 2019-3-27 14:32
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);


我错了,数组只能在定义的时候使用Initializer List,这么基本的东西被我搞忘了;

按楼下的来吧,C语言的范畴内应该是这样写最简单了

或者这样写
  1. typedef struct record {
  2.         int x;
  3.         int y;
  4.         int z;
  5.         int n;
  6. } POINT;

  7. POINT point(int x ,int y ,int z ,int n)
  8. {
  9.         POINT p;
  10.         p.x = x ;
  11.         p.y = y ;
  12.         p.z = z ;
  13.         p.n = n ;
  14. }

  15. int main(){
  16.         POINT coordinates[4];

  17.         coordinates[0]=point(5,7,9,0);
  18.         coordinates[1]=point(1,2,3,1);
  19.         coordinates[2]=point(2,4,7,2);
  20.         coordinates[3]=point(5,8,1,3);
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 15:09:34 | 显示全部楼层
jackz007 发表于 2019-3-27 14:19
用结构数组如何:

谢谢!我先消化下!:)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-27 15:10:25 | 显示全部楼层
Croper 发表于 2019-3-27 14:56
我错了,数组只能在定义的时候使用Initializer List,这么基本的东西被我搞忘了;

按楼下的来吧,C ...

谢谢啊,我先消化下。:)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-27 18:17:24 | 显示全部楼层
老猪想回高老庄 发表于 2019-3-27 15:10
谢谢啊,我先消化下。:)


才看到是C++,
c++就简单了,直接标准库走起
  1. #include <vector>
  2. using namespace std;
  3. int main(){
  4.     vector<vector<int>> coordinates;
  5.    
  6.     coordinates[0] = {5,7,9,0};
  7.     coordinates[1] = {1,3,5,1};
  8.     coordinates[2] = {2,4,7,2};
  9.     coordinates[3] = {5,8,1,3};
  10. }
复制代码


想怎么弄怎么弄,数组大小都不用定义
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 03:41:44 | 显示全部楼层
Croper 发表于 2019-3-27 18:17
才看到是C++,
c++就简单了,直接标准库走起

非常感谢:), 我先试试这两种方法。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 11:05:49 | 显示全部楼层
本帖最后由 老猪想回高老庄 于 2019-3-28 12:00 编辑
Croper 发表于 2019-3-27 18:17
才看到是C++,
c++就简单了,直接标准库走起


你好, 我试了下好像还有点问题, 我写了如下的code,想测试一下有没有问题:
#include <Arduino.h>
#include <Streaming.h>
#include <Vector.h>

void setup()
{
  Serial.begin(9600);
  delay(1000);
  Vector<int> vector;
  vector.push_back(3);
  vector.push_back(5);
  vector.push_back(7);
  vector.push_back(3);
  vector.push_back(5);
  vector.push_back(9);
  Serial.print("Vector: ");
  Serial.print(vector[3]);  
}

编译没有问题了, 但是当我上传给arduino uno后,结果在serial monitor里面显示:
Vector: 0
好奇怪,结果应该是其中的一个值才对啊。

我又测试了以下的code:
void setup()
{
  Serial.begin(9600);
  delay(1000);
  Vector<int> vector;
  vector.push_back(3);
  vector.push_back(5);
  vector.push_back(7);
  vector.push_back(3);
  vector.push_back(5);
  vector.push_back(9);
  Serial.print("Vector Size = ");
  Serial.print(vector.size());  
}
结果在serial monitor 里显示:
Vector Size = 0
应该是6才对啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-28 13:41:23 | 显示全部楼层
查了一下,
https://www.arduino.cn/thread-67356-1-1.html
arduino的c++是不自带标准库的,不然你就用前面的方法,不然就按上面的添加一个标准库吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-3-28 14:14:21 | 显示全部楼层
Croper 发表于 2019-3-28 13:41
查了一下,
https://www.arduino.cn/thread-67356-1-1.html
arduino的c++是不自带标准库的,不然你就用前 ...

好的,非常感谢你的帮助:)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 15:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表