Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
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.

Réponse acceptée

Stephen23
Stephen23 le 22 Sep 2017
Modifié(e) : Stephen23 le 22 Sep 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 commentaires
MSP
MSP le 22 Sep 2017
Yeah sorry guys I had posted the question rather casually.

Connectez-vous pour commenter.

Plus de réponses (3)

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

Cedric
Cedric le 21 Sep 2017
Modifié(e) : Cedric le 21 Sep 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) ;
  2 commentaires
MSP
MSP le 21 Sep 2017
No,would you check out my previous comment on Image Analyst answer

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 21 Sep 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 commentaires
MSP
MSP le 21 Sep 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 le 22 Sep 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Resizing and Reshaping Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by