How to normalize rows using retmap.
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (1)
Adam Danz
am 16 Sep. 2021
Bearbeitet: Adam Danz
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!