将 3d 矩阵重塑为 2d 矩阵

发布于 2024-08-21 08:32:17 字数 196 浏览 6 评论 0原文

我在 MATLAB 中有一个 3d 矩阵 (n-by-m-by-t),表示一段时间内网格中的 n-by-m 测量值。我想要一个二维矩阵,其中空间信息消失了,仅留下 n*m 随着时间 t 的测量(即:n*m- by-t

我该怎么做?

I have a 3d matrix (n-by-m-by-t) in MATLAB representing n-by-m measurements in a grid over a period of time. I would like to have a 2d matrix, where the spatial information is gone and only n*m measurements over time t are left (ie: n*m-by-t)

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拍不死你 2024-08-28 08:32:17

您需要命令 reshape

说你的初始矩阵是(只是为了让我获取一些数据):

a=rand(4,6,8);

然后,如果最后两个坐标是空间坐标(时间是4,m是6,n是8),你使用:

a=reshape(a,[4 48]);

并且最终得到一个4x48数组。

如果前两个是空间的,最后一个是时间(m 为 4,n 为 6,时间为 8),则使用:

a=reshape(a,[24 8]);

最终得到一个 24x8 数组。

这是一个快速的 O(1) 操作(它只是调整数据形状的标头)。还有其他方法可以做到这一点,例如 a=a(:,:) 来压缩最后两个维度,但重塑速度更快。

You need the command reshape:

Say your initial matrix is (just for me to get some data):

a=rand(4,6,8);

Then, if the last two coordinates are spatial (time is 4, m is 6, n is 8) you use:

a=reshape(a,[4 48]);

and you end up with a 4x48 array.

If the first two are spatial and the last is time (m is 4, n is 6, time is 8) you use:

a=reshape(a,[24 8]);

and you end up with a 24x8 array.

This is a fast, O(1) operation (it just adjusts it header of what the shape of the data is). There are other ways of doing it, e.g. a=a(:,:) to condense the last two dimensions, but reshape is faster.

痴者 2024-08-28 08:32:17

Reshape 当然是重塑数组的标准解决方案。 (他们还会叫它什么?)还有一些技巧有待揭开。

首先,将大小为 [n,m,p] 的数组转换为大小为 [n*m,p] 的数组最简单的方法是什么?

B = reshape(A,n*m,p);

但更好的是:

B = reshape(A,[],p);

如果您将 reshape 的参数之一留空,则 reshape 会为您计算大小!小心,如果你尝试这样做并且 A 的大小不符合,那么你会得到一个错误。因此:从

reshape(magic(3),[],2)
??? Error using ==> reshape
Product of known dimensions, 2, not divisible into total number of elements, 9.

逻辑上讲,我们不能从包含 9 个元素的数组中创建一个包含两列的数组。我确实在 MATLAB Central 交换上放置了一个名为 wreshape 的函数,该函数将根据需要进行填充执行此操作不会产生错误。

当然,您始终可以使用一些技巧,例如

B = A(:);

直接从矩阵创建向量。这相当于形式

B=reshape(A,[],1);

Reshape is of course the standard solution to reshaping an array. (What else would they call it?) There are still a few tricks to uncover.

First of all, the simplest way to turn an array of size [n,m,p] into an array of size [n*m,p]?

B = reshape(A,n*m,p);

But better is this:

B = reshape(A,[],p);

If you leave one of the arguments to reshape empty, then reshape computes the size for you! Be careful, if you try this and the size of A does not conform, then you will get an error. Thus:

reshape(magic(3),[],2)
??? Error using ==> reshape
Product of known dimensions, 2, not divisible into total number of elements, 9.

Logically, we cannot create an array of with two columns from something that has 9 elements in it. I did put a function called wreshape on the MATLAB Central exchange that would pad as necessary to do this operation with no error generated.

Of course, you can always use tricks like

B = A(:);

to create a vector directly from a matrix. This is equivalent to the form

B=reshape(A,[],1);

解决此问题的更好方法是使用 Rasmus Bro 的 N-Way Toolbox 中提供的 nshape.m。这将允许您沿着给定模式重塑,这对于重塑命令来说有点棘手。

该工具箱可通过以下链接获取:

https://www. mathworks.com/matlabcentral/fileexchange/1088-the-n-way-toolbox

还有一些其他优秀的工具可以帮助进行数组操作。

A better solution to this problem is to use nshape.m available from Rasmus Bro's N-Way Toolbox. That will allow you to reshape along a given mode, which is a little more tricky with the reshape command.

The toolbox is available at the following link:

https://www.mathworks.com/matlabcentral/fileexchange/1088-the-n-way-toolbox

There are some other excellent tools there to help with array manipulation as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文