How to find the location of values in larger arrays where the value to the right is greater
Info
This question is closed. Reopen it to edit or answer.
Show older comments
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 :)
Answers (2)
Guillaume
on 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
on 15 Nov 2014
out=[diff(a,[],2) zeros(size(a,1),1)]>0
2 Comments
Oliver
on 28 Nov 2014
Azzi Abdelmalek
on 28 Nov 2014
Edited: Azzi Abdelmalek
on 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
This question is closed.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!