How to find the location of values in larger arrays where the value to the right is greater
Infos
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Afficher commentaires plus anciens
I am struggling to develop a way of creating a matrix which displays the locations of when the value to the right is greater than the value itself. I want an output array of 1's and 0's to be displayed - 1 if the value to the right is greater
e.g. for the matrix:
20 10 10 10 10 10
10 10 -3 10 10 10
10 20 10 10 20 -6
10 10 20 10 10 10
20 10 10 10 10 25
I would like a script which writes when the value to the right is greater (displayed by a '1'):
0 0 0 0 0 0
0 0 1 0 0 0
1 0 0 1 0 0
0 1 0 0 0 0
0 0 0 0 1 0
What would the script look like, if instead a larger value to the left is required?
Thanks for any answers :)
Réponses (2)
Guillaume
le 15 Nov 2014
Use diff on rows for that. diff will be positive for values that are greater on the right. To get a matrix the same size as the original, just concatenate a column of 0 to the right:
m = [20 10 10 10 10 10
10 10 -3 10 10 10
10 20 10 10 20 -6
10 10 20 10 10 10
20 10 10 10 10 25];
g = [diff(m, 1, 2) > 0, zeros(size(m, 1), 1)]
Azzi Abdelmalek
le 15 Nov 2014
out=[diff(a,[],2) zeros(size(a,1),1)]>0
2 commentaires
Oliver
le 28 Nov 2014
Azzi Abdelmalek
le 28 Nov 2014
Modifié(e) : Azzi Abdelmalek
le 28 Nov 2014
out=fliplr([diff(fliplr(a),[],2) zeros(size(a,1),1)]>0)
%or
out1=[ zeros(size(a,1),1) diff(a,[],2) ]<0
Cette question est clôturée.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!