How to find the location of values in larger arrays where the value to the right is greater
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
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 :)
Antworten (2)
Guillaume
am 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
am 15 Nov. 2014
out=[diff(a,[],2) zeros(size(a,1),1)]>0
2 Kommentare
Oliver
am 28 Nov. 2014
Azzi Abdelmalek
am 28 Nov. 2014
Bearbeitet: Azzi Abdelmalek
am 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
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!