How to normalize rows using retmap.
조회 수: 10 (최근 30일)
이전 댓글 표시
I have a matrix A, where I would like to normalize all of the rows such that the sum of each individual row is 1 using retmap. If we have [1,2,3] --> [1/6, 2/6, 3/6]
My current approach is to loop through the matrix A, and grab the size of each row. For example.
[c d] = size(A)
for i=1:c
s = sum(A(i,;))
end
How would I utilize the retmap function such that we complete this function
댓글 수: 0
답변 (1개)
Adam Danz
2021년 9월 16일
편집: Adam Danz
2021년 9월 17일
I don't know what retmap is (did you mean repmat?)
Anyway, this normalizes the matrix by rows as you described,
% sample data
data = rand(20) .* randi(100,20,1)
% Normalize rows of 'data' so each row sums to 1
dataNorm = data./sum(data,2)
% confirm by adding values in each row
% The asser() will throw an error if any row does
% sum to 1, leaving room for precision error.
addedRows = sum(dataNorm,2);
assert(all(abs(addedRows-1)<1E10), 'Santify check failed: normalization is incorrect.')
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!