简体   繁体   English

MATLAB:从选定的矩阵元素构造矩阵

[英]MATLAB: Construct matrix from selected matrix elements

I have a 3x3x3 matrix which contains a particular set of elements that I would like to extract.我有一个 3x3x3 矩阵,其中包含我想提取的一组特定元素。 However, I would like for the elements to be ordered in a matrix after selecting them.但是,我希望在选择元素后按矩阵排序元素。 An example is:一个例子是:

a(1,:,:)=1*[1 2 3; 4 5 6; 7 8 9];
a(2,:,:)=2*[1 2 3; 4 5 6; 7 8 9];
a(3,:,:)=3*[1 2 3; 4 5 6; 7 8 9];

a(a>1.0)

The condition a(a>1.0) gives me a vector of elements, but is there a way to order them in a matrix following their original ordering?条件a(a>1.0)给了我一个元素向量,但是有没有办法按照它们的原始顺序在矩阵中对它们进行排序?

What would you like to do to the elements that don't satisfy your criteria?您想对不符合您标准的元素做什么?

You could do something like a(a<=1) = nan;你可以做类似a(a<=1) = nan;

Suppose 2-D matrix for simplicity:为简单起见假设二维矩阵:

a = [1 2 3; ...
     4 5 6];

Let's take only even values and keep them in their original shape:让我们只取数值并保持它们的原始形状:

a(mod(a, 2) == 0)

You might want:你可能想要:

  2
4   6

However, in the world of matrix, is there such a matrix which has empty space?然而,在矩阵的世界中,有没有这样一个空旷的矩阵呢? Yes, a sparse matrix.是的,稀疏矩阵。 But, you must note that a sparse matrix is filled with 0 not just missing.但是,您必须注意,稀疏矩阵填充了0而不仅仅是缺失。

So, my suggestion is to replace other values with NaN所以,我的建议是用NaN替换其他值

b = a; % just make a duplicate
b(mod(b, 2) == 1) = nan
b =
    nan    2    nan
      4  nan      6    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM