码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • matlab复制矩阵到c


    方法1:运行脚本,选中matlab中多行多列矩阵的数据复制,输出线性排列的数据(先行后列),按英文逗号隔开,在c文件中粘贴使用。
    import os, re, sys, time, pyperclip 
    
    
    def convert():
        print("Exit: ctrl+c or copy 'q'\n")
        recent = ""
        tmp = ""  
        while True:
            tmp = pyperclip.paste() 
            
            if tmp == 'q': 
                break
    
            try:
                if tmp != recent:  
                    recent = tmp
                    
                    changed = re.sub(r"\s{2,}", ", ", recent)  # matlab纵向
                    changed = changed.replace("\t", ", ")  #matlab横向
                    pyperclip.copy(changed)  
                    print("Converted:\n%s\n" % str(changed))
                    
                    recent = changed
                time.sleep(0.1)
            except KeyboardInterrupt: 
                break
    
    
    if __name__ == "__main__":
        convert()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    方法2:创建txt文件,直接复制到txt文件中(会用空格和换行符隔开),之后用c函数从中读取
    typedef struct {
    	double** mat;
    	int row, col;
    }Matrix2d;
    
    Matrix2d initMatrix2d(int row, int col, double* X) {
    	Matrix2d M;
    	M.row = row;
    	M.col = col;
    	M.mat = (double**)malloc(sizeof(double*) * row);
    	for (int i = 0; i < row; i++) {
    		M.mat[i] = (double*)malloc(sizeof(double) * col);
    	}
    
    	if (X != 0) {
    		for (int i = 0; i < row; i++) {
    			for (int j = 0; j < col; j++) {
    				M.mat[i][j] = *(X + i * col + j);
    			}
    		}
    	}
    
    	return M;
    }
    
    void getdatafromtxt(Matrix2d m, char* filename) {
    	int row = m.row, col = m.col;
    
    	FILE* fpin_amp;
    	fopen_s(&fpin_amp, filename, "rt");
    
    	for (int i = 0; i < row; i++) {
    		for (int j = 0; j < col; j++) {
    			fscanf_s(fpin_amp, "%lf", &m.mat[i][j]);
    		}
    	}
    }
    
    
    void main() {
    	Matrix2d m = initMatrix2d(3, 3, 0);
    	char* filename = "data.txt";
    	getdatafromtxt(m, filename);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    其中,示例的data.txt
    在这里插入图片描述

    tips:对于数据量过大的矩阵,直接赋值静态初始化会导致栈溢出,推荐方法2

  • 相关阅读:
    uboot移植之环境变量bootcmd
    小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
    图 Dijkstra / Bellman-Ford / Floyd-Warshell Leetcode 743-Network Delay Time
    黑猫带你学Makefile第4篇:Makefile中变量的使用
    DSPE-PEG-Thiol DSPE-PEG-SH 磷脂-聚乙二醇-巯基脂质体制备用
    周赛总结--LeetCode单周赛321场 && AcWing79场
    java spring cloud 企业工程管理系统源码+二次开发+定制化服务
    免费api接口集合分享,再也不怕找不到免费api了
    【工作技能】如何制作有效的简历
    企业微信hook接口协议,根据用户id批量获取详细信息
  • 原文地址:https://blog.csdn.net/qq_43027065/article/details/127960311
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号