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

2 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Stephen23
Stephen23 il 22 Set 2017
Modificato: Stephen23 il 22 Set 2017
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 Commenti

Accedi per commentare.

Più risposte (3)

Andrei Bobrov
Andrei Bobrov il 22 Set 2017
B = cummax(A);
A([false(1,size(A,2));diff(B)==0]) = nan;

Cedric
Cedric il 21 Set 2017
Modificato: Cedric il 21 Set 2017
>> 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) ;

Image Analyst
Image Analyst il 21 Set 2017
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 Commenti
MSP
MSP il 21 Set 2017
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 il 22 Set 2017
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.

Accedi per commentare.

Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by