我如何获得一个N-D数组作为二维数组的最后两个维度? [英] How do I get the two last dimensions of an N-D array as a 2D array?

查看:371
本文介绍了我如何获得一个N-D数组作为二维数组的最后两个维度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB三维数组,尺寸(myarray的)= [100 100 50] 。现在,我想获得一个特定的层,由索引在第一个的尺寸规定,在一个二维矩阵的形式。
我试图于myMatrix = myArray的(myIndex,:,:); ,但是这给了我与尺寸(于myMatrix)= [1 100 50三维数组]

I have a 3D array in MATLAB, with size(myArray) = [100 100 50]. Now, I'd like to get a specific layer, specified by an index in the first dimension, in the form of a 2D matrix. I tried myMatrix = myArray(myIndex,:,:);, but that gives me a 3D array with size(myMatrix) = [1 100 50].

我如何告诉MATLAB我不感兴趣的第一维(因为只有一层),因此它可以简化矩阵?

How do I tell MATLAB that I'm not interested in the first dimension (since there's only one layer), so it can simplify the matrix?

请注意:我需要用第二个索引要做到这一点也使尺寸(于myMatrix)= [100 1 50] ,而不是期望的 [100 50] 。一个解决方案应适用于这两种情况下,和preferably到第三个层面为好。

Note: I will need to do this with the second index also, rendering size(myMatrix) = [100 1 50] instead of the desired [100 50]. A solution should be applicable to both cases, and preferably to the third dimension as well.

推荐答案

使用 紧缩 功能,从而消除单维度。

Use the squeeze function, which removes singleton dimensions.

示例:

A=randn(4,50,100);
B=squeeze(A(1,:,:));
size(B)

ans =

    50   100

这是广义的,你不用担心哪个维度你一起索引。所有单身尺寸挤掉。

This is generalized and you needn't worry about which dimension you're indexing along. All singleton dimensions are squeezed out.

这篇关于我如何获得一个N-D数组作为二维数组的最后两个维度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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