0

I am new to Matlab

I am trying to do something to an 2x3 array A:

  1. add 10 to the highest value of A;
  2. add 6 to the second highest value of A
  3. add 4 to the third highest value of A
  4. add 1 to the minimal value of A

for example:

A = [13 14; 19 17; 54 33];

output :[14 14; 23 17; 64 39];

is there any chance to achieve this without knowing the elements' value inside the array?

help please

1 Answer 1

1

Without knowing the elements, we could just get indexs of them by calling [~,I] = sort(___) and call A(I(k)) to find the k-th number in matrix A.

[B,I] = sort(___) will return a collection of index vectors for any of the previous syntaxes. I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension. For example, if A is a numeric vector, B = A(I).

%data
A = [13 14; 19 17; 54 33];

%sort
[~,index]=sort(A(:));

%add
A(index(end))=A(index(end))+10;
A(index(end-1))=A(index(end-1))+6;
A(index(end-2))=A(index(end-2))+4;
A(index(1))=A(index(1))+1;
A

Ref:

doc sort
Sign up to request clarification or add additional context in comments.

4 Comments

@dark.o Glad that I could help. Besides, you should ask MATLAB first in the next time, since help and doc are the excellent guides for newcomers.
ok~~ I did read through the docs before posting the question though ^^
Oh, sorry. I apologize for my words.
It's ok. Your answer helps me a lot. Thanks ~~

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.