Effacer les filtres
Effacer les filtres

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

6 vues (au cours des 30 derniers jours)
Vishal Sharma
Vishal Sharma le 19 Juin 2017
Modifié(e) : Jan le 19 Juin 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]

Réponse acceptée

Guillaume
Guillaume le 19 Juin 2017
Modifié(e) : Guillaume le 19 Juin 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.

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by