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中有一个3D数组,有size(myArray) =[100 100 50]。现在,我想要得到一个特定的层,由一维中的索引指定,以二维矩阵的形式。我尝试了myMatrix = myArray(myIndex,:,:);,但这给了我一个size(myMatrix) =[110050]的3D数组。
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?
我如何告诉MATLAB我对第一维不感兴趣(因为只有一层),这样它可以简化矩阵?
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.
注意:我还需要对第二个索引执行此操作,即呈现大小(myMatrix) =[100150],而不是期望的[10050]。解决方案应该适用于这两种情况,最好也适用于第三维。
3 个解决方案
#1
14
Use the squeeze
function, which removes singleton dimensions.
使用挤压函数,它删除单元素维度。
Example:
例子:
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.
这是泛化的,您不必担心正在索引的维度。所有的单例维度都被挤出。
#2
2
reshape(myArray(myIndex,:,:),[100,50])
#3
0
squeeze
, reshape
and permute
are probably the three most important functions when dealing with N-D matrices. Just to have an example how to use the third function:
在处理N-D矩阵时,挤压、重塑和排列可能是最重要的三个函数。举个例子,如何使用第三个函数:
A=randn(4,50,100);
B=permute(A(1,:,:),[2,3,1])
#1
14
Use the squeeze
function, which removes singleton dimensions.
使用挤压函数,它删除单元素维度。
Example:
例子:
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.
这是泛化的,您不必担心正在索引的维度。所有的单例维度都被挤出。
#2
2
reshape(myArray(myIndex,:,:),[100,50])
#3
0
squeeze
, reshape
and permute
are probably the three most important functions when dealing with N-D matrices. Just to have an example how to use the third function:
在处理N-D矩阵时,挤压、重塑和排列可能是最重要的三个函数。举个例子,如何使用第三个函数:
A=randn(4,50,100);
B=permute(A(1,:,:),[2,3,1])