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

조회 수: 2 (최근 30일)
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

채택된 답변

the cyclist
the cyclist 2022년 3월 27일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by