Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何在给定范围内获得大于x的元素?_Arrays_Matlab_Matrix - Fatal编程技术网

Arrays 如何在给定范围内获得大于x的元素?

Arrays 如何在给定范围内获得大于x的元素?,arrays,matlab,matrix,Arrays,Matlab,Matrix,给定一个矩阵a,如何使元素(及其索引)在特定范围内大于x e、 g 例如,我希望所有元素都大于5,并且出现在范围A(2:4,3:5)中。我应该得到: 要素: 6 , 6 , 7 , 6 , 7 , 8 指数: 14, 18, 19, 22, 23, 24 A(A>5)会给我所有大于5的条目 A(2:4,3:5)将给出范围2:4,3:5中的所有元素 我想把两者结合起来。是否可能或唯一的方法是将所需的范围放入另一个数组B,然后执行B(B>5)?显然这里有两个问题:我会丢失原始索引,而且速度会慢一

给定一个矩阵
a
,如何使元素(及其索引)在特定范围内大于
x

e、 g

例如,我希望所有元素都大于5,并且出现在范围
A(2:4,3:5)
中。我应该得到:

要素:

6 , 6 , 7 , 6 , 7 , 8
指数:

14, 18, 19, 22, 23, 24
A(A>5)
会给我所有大于5的条目

A(2:4,3:5)
将给出范围
2:4,3:5
中的所有元素

我想把两者结合起来。是否可能或唯一的方法是将所需的范围放入另一个数组
B
,然后执行
B(B>5)
?显然这里有两个问题:我会丢失原始索引,而且速度会慢一些。我在大量矩阵上执行此操作。

如果您只需要值(而不是索引),可以使用
find
和矩阵乘法的第三个输出来完成。我不知道它是否会比使用临时阵列更快,不过:

[~, ~, values] = find((A(2:4,3:5)>5).*A(2:4,3:5));
假设您需要线性索引和值,那么如果阈值为正,您可以定义一个掩码。如果掩码可以定义一次并对所有矩阵重复使用(即,如果所有矩阵的所需范围相同),这可能是一个好主意:


有点笨重,但是:

R = 2:4;
C = 3:5;
I = reshape(find(A),size(A))

indicies = nonzeros(I(R,C).*(A(R,C)>5))
values = A(indicies)

代码。我试图避免矩阵乘法,因此这看起来可能有点奇怪:

A = [1:5; 2:6; 3:7; 4:8; 5:9];

[r,c] = meshgrid(2:4,3:5);
n     = sub2ind(size(A), r(:), c(:));

indices = sort(n(A(n) > 5)); %'skip sorting if not needed'
values  = A(indices);
解释。代码将下标的笛卡尔积转换为
A
矩阵中的线性指数。然后选择符合条件的索引,然后选择值

然而,它是缓慢的

优化。根据的建议,可以通过将基于
sub2ind
的线性索引计算替换为手工制作的线性索引计算来加快代码的速度:

A = [1:5; 2:6; 3:7; 4:8; 5:9];

%'For column-first, 1-based-index array memory   '
%'layout, as in MATLAB/FORTRAN, the linear index '
%'formula is:                                    '
%'L = R + (C-1)*NR                               '
n = bsxfun(@plus, (2:4), (transpose(3:5) - 1)*size(A,1)); 

indices = n(A(n) > 5);
values  = A(indices);

有些相关:。所以答案是否定的,除非你使用一些丑陋的技巧。指数实际上更重要。不过我会调查答案的。ThanksHow如何使
遮罩稀疏?如果有一个小的测试窗口,@CST-Link对于内存效率来说是个好主意!至于速度,我不太确定这能给出指数吗?哇!太快了!比使用临时矩阵快2.5倍!可能更快:
n=bsxfun(@plus,(2:4),((3:5)。'-1)*大小(A,1))
代替
meshgrid
sub2ind
@LuisMendo用手工制作的线性指数计算取代
meshgrid
sub2ind
这两个昂贵的调用的确是个好主意。@AlaaM。针对相关问题,对
meshgrid/ndgrid+sub2ind
vs
bsxfun
进行了基准比较,对使用
bsxfun
提高性能的可能原因缺乏深入了解。
A = [1:5; 2:6; 3:7; 4:8; 5:9];

[r,c] = meshgrid(2:4,3:5);
n     = sub2ind(size(A), r(:), c(:));

indices = sort(n(A(n) > 5)); %'skip sorting if not needed'
values  = A(indices);
A = [1:5; 2:6; 3:7; 4:8; 5:9];

%'For column-first, 1-based-index array memory   '
%'layout, as in MATLAB/FORTRAN, the linear index '
%'formula is:                                    '
%'L = R + (C-1)*NR                               '
n = bsxfun(@plus, (2:4), (transpose(3:5) - 1)*size(A,1)); 

indices = n(A(n) > 5);
values  = A(indices);