在Matlab中重塑矩阵 [英] Reshaping a matrix in matlab

查看:160
本文介绍了在Matlab中重塑矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在matlab中有一个28x28x10000的矩阵.我希望将其重塑为10000 * 784的矩阵,并将每个28x28子矩阵压缩成一行.所以我尝试了

I've got a matrix in matlab that's 28x28x10000. I'm looking to reshape it into a matrix that's 10000*784, with each 28x28 submatrix being squeezed into a row. So I tried

reshape(mat, 10000, 784)

这确实为我提供了正确形状的矩阵,但值不正确.有谁知道另一种方式来做到这一点,最好没有for循环?

While this does give me a matrix of the correct shape, the values aren't correct. Does anyone know of another way to do this, preferably without for loops?

推荐答案

reshape从矩阵中逐列获取元素.对您而言,这意味着如果您重新排列原始矩阵的尺寸(使用 permute ),您可以像现在一样使用reshape:

reshape take elements column-wise from the matrix. For your purpose, that means that if you rearrange the dimensions of your original matrix (using permute), you can use reshape like you already are:

reshape(permute(mat,[3 1 2]), 10000, []);

permute[3 1 2]参数意味着将第3维用作第一个,然后将原始1维用作新的第二维,并将原始2nd维用作新的第3维,从而为您提供一个10000x28x28矩阵.每列包含10000个元素,因此像reshape一样逐列进行操作将为您提供所需的内容.

The [3 1 2] argument to permute means use the 3rd dimension as the 1st, then the original 1st as the new 2nd, and the original 2nd dimension as the new 3rd one, giving you a 10000x28x28 matrix. Each column contains 10000 elements, so taking column-by-column like reshape does will work give you what you want.

这篇关于在Matlab中重塑矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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