フィルターのクリア

How to check whether a 2d matrix is gradually increasing in values in row direction.

1 回表示 (過去 30 日間)
MSP
MSP 2017 年 9 月 21 日
コメント済み: MSP 2017 年 9 月 22 日
Lets say u have a matrix A=[2 4 7;3 4 6;] So we can see the A(4)==3 in row 2 has increased from A(1)==2 progression,
And the 6th element,A(6)==6 has reduced from being A(3)==7 to 6.
So the A(6) needs to be replaced by Nan
This is basically the thing. Needs to be done in a large matrix. Any ideas on doing it faster than for loops.

採用された回答

Stephen23
Stephen23 2017 年 9 月 22 日
編集済み: Stephen23 2017 年 9 月 22 日
Using cummax is simple:
>> A = [2,4,7;3,4,6]
A =
2 4 7
3 4 6
>> A(A<cummax(A,1)) = NaN
A =
2 4 7
3 4 NaN
EDIT: to also ignore adjacent repeated values:
>> A = [2,4,7;3,4,6]
A =
2 4 7
3 4 6
>> idx = A<cummax(A,1) | 0==diff([NaN*A(1,:);A],1,1);
>> A(idx) = NaN
A =
2 4 7
3 NaN NaN
  4 件のコメント
MSP
MSP 2017 年 9 月 22 日
Yeah sorry guys I had posted the question rather casually.

サインインしてコメントする。

その他の回答 (3 件)

Andrei Bobrov
Andrei Bobrov 2017 年 9 月 22 日
B = cummax(A);
A([false(1,size(A,2));diff(B)==0]) = nan;

Cedric
Cedric 2017 年 9 月 21 日
編集済み: Cedric 2017 年 9 月 21 日
>> flags = [false( 1, size( A, 2 )); diff( A ) < 0]
flags =
2×3 logical array
0 0 0
0 0 1
>> A(flags) = NaN
A =
2 4 7
3 4 NaN
EDIT 5:40pm EST:
>> A = [5, 3, 4, 6; 2, 4, 2, 3].'
A =
5 2
3 4
4 2
6 3
>> select = any((A-permute(A,[3,2,1])) .* permute(tril(ones(size(A,1)*[1,1]),-1),[1,3,2]) < 0, 3)
select =
4×2 logical array
0 0
1 0
1 1
0 1
>> A(select) = NaN
A =
5 2
NaN 4
NaN NaN
6 NaN
and if you have an old version of MATLAB, the expansions must be performed using BSXFUN:
select = any(bsxfun(@times, bsxfun(@minus, A, permute(A, [3,2,1])), ...
permute(tril(ones(size(A, 1) * [1,1]), -1), [1,3,2])) < 0, 3) ;
  2 件のコメント
MSP
MSP 2017 年 9 月 21 日
No,would you check out my previous comment on Image Analyst answer
Cedric
Cedric 2017 年 9 月 21 日
See my edited solution.

サインインしてコメントする。


Image Analyst
Image Analyst 2017 年 9 月 21 日
Of course, simply use conv2():
A=randi(9, 10, 3)
zeroRow = zeros(1, size(A, 2))
m = [zeroRow; conv2(A, [1;-1], 'valid')]
A(m<0) = nan
  2 件のコメント
MSP
MSP 2017 年 9 月 21 日
But the code isnt doing what I wanted though.From the image I posted you can view what I mean,like the 3 should be replaced by Nan,otherwise its not maintaining the sequence of gradual increasing value.which in case of interpolation of missing values will lead to the same scenario again
Image Analyst
Image Analyst 2017 年 9 月 22 日
Well whatever was after the 9 was less than a 9, let's say it was a 1. So then the 1 goes to a NAN, but 3 is more than the 1 so it gets kept.
What you want is a moving peak detector. I don't think MATLAB has a movpeak() function but I think I saw someone make one in effect through some trick. Of course you could just to a for loop which should be fast as long as your array doesn't have millions of rows.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by