Recursive elimination of minimum number in a array

1 visualización (últimos 30 días)
Sowmya MR
Sowmya MR el 1 de Nov. de 2016
Respondida: Guillaume el 1 de Nov. de 2016
Hi All,
I have an array consisting of 4 groups: A=[1 1 1 2 2 1 1 1 2 2 3 3 4]. Here "1" appears six times, "2" four times, "3" twice and "4" once. In the first iteration, i should find the number that appears least number of times (in this case 4) and replace it with the next least appearing number (i.e replace 4 with 3). Then in the second iteration, the least appearing number is 3 so i should replace it with 2. Can someone please help with a simple code to do this?

Respuestas (1)

Guillaume
Guillaume el 1 de Nov. de 2016
Seems fairly straightforward to me. As I assume this is homework, I won't give you a complete solution but I'd use < unique> to get the list of unique numbers, a while loop based on the numel of values returned by unique and histcounts (with the option BinMethod, 'integers') to get the distribution of each numbers. sort that distribution and replace the first item by the second.

Community Treasure Hunt

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

Start Hunting!

Translated by