简体   繁体   English

从矩阵的每一行中获取前 2 个非零元素

[英]Get the first 2 non-zero elements from every row of matrix

I have a matrix A like this:我有一个这样的矩阵A

A = [ 1 0 2 4; 2 3 1 0; 0 0 3 4 ]

A has only unique row elements except zero, and each row has at least 2 non-zero elements. A除了零之外只有唯一的行元素,并且每行至少有 2 个非零元素。

I want to create a new matrix B from A ,where each row in B contains the first two non-zero elements of the corresponding row in A .我想从A创建一个新矩阵B ,其中B中的每一行都包含A中相应行的前两个非零元素。

B = [ 1 2 ; 2 3 ; 3 4 ]

It is easy with loops but I need vectorized solution.循环很容易,但我需要矢量化解决方案。

Here's a vectorized approach:这是一种矢量化方法:

A = [1 0 2 4; 2 3 1 0; 0 0 3 4]; % example input
N = 2; % number of wanted nonzeros per row
[~, ind] = sort(~A, 2); % sort each row of A by the logical negation of its values.
% Get the indices of the sorting
ind = ind(:, 1:N); % keep first N columns
B = A((1:size(A,1)).' + (ind-1)*size(A,1)); % generate linear index and use into A

Here is another vectorised approach.这是另一种矢量化方法。

A_bool = A > 0;   A_size = size(A);   A_rows = A_size(1);
A_boolsum = cumsum( A_bool, 2 ) .* A_bool;   % for each row, and at each column,
                                             % count how many nonzero instances
                                             % have occurred up to that column
                                             % (inclusive), and then 'zero' back
                                             % all original zero locations.

[~, ColumnsOfFirsts  ] = max( A_boolsum == 1, [], 2 );
[~, ColumnsOfSeconds ] = max( A_boolsum == 2, [], 2 );

LinearIndicesOfFirsts  = sub2ind( A_size, [1 : A_rows].', ColumnsOfFirsts  );
LinearIndicesOfSeconds = sub2ind( A_size, [1 : A_rows].', ColumnsOfSeconds );

Firsts  = A(LinearIndicesOfFirsts );
Seconds = A(LinearIndicesOfSeconds);

Result = horzcat( Firsts, Seconds )
% Result = 
%    1   2
%    2   3
%    3   4

PS. PS。 Matlab / Octave common subset compatible code. Matlab / 八度通用子集兼容代码。

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

相关问题 计算矩阵每行中的非零元素 - Count non-zero elements in every row of matrix 稀疏矩阵每一行中非零元素的平均值 - The mean value of non-zero elements in each row of a sparse matrix Python:在将每行与矩阵中的每隔一行进行比较后,存储非零唯一行的索引 - Python: Store indices of non-zero unique rows after comparing each rows with every other row in a matrix 找到给定矩阵的每一行中最后一个非零元素的索引? - Find the index of the last non-zero element in each row of a given matrix? 识别 numpy 中组内的第一个和最后一个非零元素/索引 - Identify the first & last non-zero elements/indices within a group in numpy 计算2D NumPy数组中每行和每列内的非零元素 - Counting non-zero elements within each row and within each column of a 2D NumPy array 一种计算每列或一行非零元素平均值的有效方法 - An efficient way to calculate the mean of each column or row of non-zero elements 如何在不使用循环的情况下用列/行索引替换 (0,1) numpy 数组的非零元素? - How to replace non-zero elements of a (0,1) numpy array by it's column/ row indices without using a loop? 第一组非零值(通过忽略零的单次出现) - First group of non-zero values (by neglecting single occurrence of zero) 反转 numpy 中的非零子矩阵,返回原始形状的矩阵 - Invert non-zero submatrix in numpy, returning a matrix of original shape
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM