[英]Last positive element per row in a Matlab matrix
I would like to find the last positive element per row in a matrix X
of dimension wxy
in Matlab. 我想在Matlab中找到维度为wxy
的矩阵X
中每行的最后一个正元素。
rng default;
%if w=1, then the code below works
A=sort(randn(1,20), 'descend');
idx=find(A>=0, 1, 'last');
%if w>1, how can I proceed?
A=sort(randn(8000,20),2, 'descend');
%idx=?
%I am expecting idx=[12;5;8;...]
Could you help me with a very efficient code? 您能帮我提供高效的代码吗?
The general answer to your title is harder than the answer to your specific case. 标题的一般答案比具体案例的答案难。 In your example you seem to be asking for the "last positive element per row in a Matlab matrix in which each row is sorted in descending order ". 在您的示例中,您似乎在要求“ Matlab矩阵中每行的最后一个正元素,其中每行以降序排列 ”。 This is equivalent to asking for "the smallest positive value in each row" which can be accomplished without sorting: 这等效于要求“每行中的最小正值”,而无需进行排序即可实现:
function [val, ind] = smallest_positive(A, dim)
if nargin < 2, A = A(:); dim = 1; end
A(A <= 0) = inf;
[val, ind] = min(A, [], dim);
Example of use: 使用示例:
>> A = randn(3, 8)
A =
0.7990 0.2120 -0.7420 0.3899 -0.5596 0.7812 -0.2656 0.9863
0.9409 0.2379 1.0823 0.0880 0.4437 0.5690 -1.1878 -0.5186
-0.9921 -1.0078 -0.1315 -0.6355 -0.9499 -0.8217 -2.2023 0.3274
>> [val, ind] = smallest_positive(A, 2)
val =
0.2120
0.0880
0.3274
ind =
2
4
8
Note that this returns the "last" positive value on each row only in the sense of "the value that would be last if you were to do the sort
". 请注意,这仅在“如果要进行sort
将是最后一个值”的意义上返回每行的“最后一个”正值。 If you literally want the last positive value on each row, whether sorted or not, then Divakar's answer is the way to go. 如果您确实希望每一行上的最后一个正值(无论是否排序),那么Divakar的答案就是解决之道。
rng default;
A=sort(randn(8000,20),2, 'descend');
idx = sum(A>=0, 2);
Actually you don't need to sort. 实际上,您不需要排序。
A = randn(8000,20);
idx = sum(A>=0, 2);
The most simple solution I can think of (given A
is sorted): 我能想到的最简单的解决方案(给定A
):
[~,idx] = min(A>=0,[],2); % Returns the first indices of the zero entries
idx = idx-1; % Get the indices before the first zero entries
Based on the question title I am solving for a generic case and making no assumption about the inputs being sorted or otherwise. 基于问题标题,我正在解决一个通用案例,并且不对输入进行排序或其他假设。
The idea here is to flip the rows and compare against zero
and then get argmax
along each row and then compensate for the flipping by subtracting from row length - 这里的想法是翻转行并与zero
进行比较,然后沿每行获得argmax
,然后通过减去argmax
补偿翻转-
[~,idx] = max(a(:,end:-1:1)>=0,[],2);
out = size(a,2) - idx + 1
To get the corresponding elements, simply get the linear indices and index - 要获取相应的元素,只需获取线性索引和索引-
a_last = a((out-1)*size(a,1) + [1:size(a,1)]')
Sample run - 样品运行-
>> a
a =
1.6110 0.0854 -0.8777 0.6078 0.0544 -0.4089 0.0675
0.7708 1.6510 0.1572 -0.7475 0.0218 -0.8292 1.0934
-0.4704 1.2351 1.2660 2.2117 -0.3616 -0.9500 -0.7682
0.8539 -0.5427 -1.0213 0.2489 -1.6312 0.0723 0.1284
1.5050 1.4430 1.1947 0.2846 -1.2621 0.5518 1.4290
0.1785 1.1087 -0.0225 1.1447 0.2316 -0.2431 -1.2750
0.3089 1.5716 -1.9958 0.0015 1.5448 -0.0750 0.4965
0.3593 0.8143 0.4389 -0.2541 0.1558 -0.2965 0.7111
>> [~,idx] = max(a(:,end:-1:1)>=0,[],2);
>> out = size(a,2) - idx + 1
out =
7
7
4
7
7
5
7
7
>> a((out-1)*size(a,1) + [1:size(a,1)]')
ans =
0.0675
1.0934
2.2117
0.1284
1.4290
0.2316
0.4965
0.7111
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.