필터 지우기
필터 지우기

how to replace the values of a matrix?

조회 수: 2 (최근 30일)
Sahar abdalah
Sahar abdalah 2015년 4월 16일
댓글: Sahar abdalah 2015년 4월 16일
Hello everyone, I have a matrix A : size m*n (4 * 10) and I want to operate this matrix line by line .
A=[0.82 0.36 0.25 0.95 0.66 0.88 0.96 0.11 0.55 0.47
0.25 0.55 0.33 0.85 0.99 0.37 0.21 0.12 0.44 0.66
0.85 0.11 0.65 0.89 0.59 0.24 0.57 0.33 0.45 0.90
0.38 0.54 0.85 0.44 0.21 0.69 0.11 0.22 0.02 0.88]
for each row of A, I want to find the maximum and replace by n, then look up again and replace by n-1 and so on. for example, the maximum value of the first row of the matrix A is 0.96 so I replaced it with 10 after another maximum value is 0.95, it will be replaced by 9 and the same for other values. Finally, I want to get a new matrix A.
A=[7 3 2 9 6 8 10 1 5 4
3 7 4 9 10 5 2 1 6 8
8 1 7 9 6 2 5 3 4 10
5 7 9 6 3 8 2 4 1 10]
please helpe me, how can I implement this problem in matlab? and thanks in advance.

채택된 답변

Guillaume
Guillaume 2015년 4월 16일
Use the second output of sort to help you:
newA = zeros(size(A));
for rowidx = 1:size(A, 1)
row = A(rowidx, :);
[~, order] = sort(row); %get the order of the values in the row
newA(rowidx, order) = 1:numel(row); %and use that order to replace the elements by 1:10
end
  댓글 수: 1
Sahar abdalah
Sahar abdalah 2015년 4월 16일
thank you, that's exactly what I want

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by