How to normalize rows using retmap.
9 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Adam Danz
on 16 Sep 2021
Edited: Adam Danz
on 17 Sep 2021
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 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!