Clear Filters
Clear Filters

How to replace minimum values with 0 and maximum with 1 in an array

6 views (last 30 days)
I have an array of
A = [ 1 4 3 4 3 4 5 6 4 5 6 7;
2 3 4 5 3 4 5 6 2 3 4 2]
I want to replace maximum value in sets of four value with ‘1’ and remaining with ‘0’ of each row. So, as to get result as follows:--
A = [ 0 1 0 0 0 0 0 1 0 0 0 1;
0 0 0 1 0 0 0 1 0 0 1 0]

Accepted Answer

Guillaume
Guillaume on 19 Jun 2017
Edited: Guillaume on 19 Jun 2017
What you didn't say is that your set of four values is by row. Matlab works by column. You'll make your life much easier if you work the same way as matlab. In this particular case, it would be even easier if you stored your array in columns of 4 rows.
A = [ 1 4 3 4 3 4 5 6 4 5 6 7; 2 3 4 5 3 4 5 6 2 3 4 2]
[~, maxloc] = max(reshape(A', 4, [])); %transpose to work by column instead of row
maxloc = sub2ind([4, numel(maxloc)], maxloc, 1:numel(maxloc));
A = zeros(size(A'));
A(maxloc) = 1;
A = A' %transpose back
Much of the work above is just to get the array in the correct shape and back to the shape you have.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!