How to replace minimum values in the vector with 1 and replace others value plus one

1 view (last 30 days)
NA
NA on 27 Mar 2022
Answered: the cyclist on 27 Mar 2022
I have a vector A
A = [2; 1; 4; 1; 3; 3; 1; 1];
In this vector I want to find maximum and put the value to 1
[B,I] = sort(A,'descend') ;
B = [4; 3; 3; 2; 1; 1; 1; 1]
B new_B
----------------
4 ---> 1
For other values, we should have previous value+1
B new_B
---------------
4 ---> 1
3 ---> 1+1=2= previous value+1
3 ---> 1+1=2
2 ---> 2+1=3= previous value+1
1 ---> 3+1=4= previous value+1
1 ---> 3+1=4
1 ---> 3+1=4
1 ---> 3+1=4
If we consider vector A the result should be:
result = [3; 4; 1; 4; 2; 2; 4 ; 4]
2--->3 2 should replace with 3
1--->4 1 should replace with 4
4--->1 4 should replace with 1

Accepted Answer

the cyclist
the cyclist on 27 Mar 2022
This might be too specific to your example, but a much simpler transform that gets what you want is
A = [2; 1; 4; 1; 3; 3; 1; 1];
result = max(A) + 1 - A
result = 8×1
3 4 1 4 2 2 4 4

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by