在MATLAB中展开矩阵的最快方法是什么? [英] What's the fastest way to unroll a matrix in MATLAB?

查看:328
本文介绍了在MATLAB中展开矩阵的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何转换矩阵:

[ 0.12 0.23 0.34 ;
  0.45 0.56 0.67 ;
  0.78 0.89 0.90 ] 

到具有一排行的坐标"矩阵中?

into a 'coordinate' matrix with a bunch of rows?

[ 1 1 0.12 ;
  1 2 0.23 ;
  1 3 0.34 ;
  2 1 0.45 ;
  2 2 0.56 ;
  2 3 0.67 ;
  3 1 0.78 ;
  3 2 0.89 ;
  3 3 0.90 ]

(行的排列无关紧要,只有数据在此结构中才重要)

(permutation of the rows is irrelevant, it only matters that the data is in this structure)

现在我正在使用for循环,但这需要很长时间.

Right now I'm using a for loop but that takes a long time.

推荐答案

以下是使用ind2sub的选项:

mat= [ 0.12 0.23 0.34 ;
  0.45 0.56 0.67 ;
  0.78 0.89 0.90 ] ;

[I,J] = ind2sub(size(mat), 1:numel(mat));
r=[I', J', mat(:)]

r =

    1.0000    1.0000    0.1200
    2.0000    1.0000    0.4500
    3.0000    1.0000    0.7800
    1.0000    2.0000    0.2300
    2.0000    2.0000    0.5600
    3.0000    2.0000    0.8900
    1.0000    3.0000    0.3400
    2.0000    3.0000    0.6700
    3.0000    3.0000    0.9000

请注意,与您的示例相比,索引是相反的.

Note that the indices are reversed compared to your example.

这篇关于在MATLAB中展开矩阵的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆