How to replace minimum values with 0 and maximum with 1 in an array
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Vishal Sharma
el 19 de Jun. de 2017
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]
0 comentarios
Respuesta aceptada
Guillaume
el 19 de Jun. de 2017
Editada: Guillaume
el 19 de Jun. de 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.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Operators and Elementary Operations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!