MATLAB Answers

How to normalize rows using retmap.

1 view (last 30 days)
Casey Park
Casey Park on 16 Sep 2021
Edited: Adam Danz on 17 Sep 2021
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

Answers (1)

Adam Danz
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)
data = 20×20
4.2582 21.2749 13.4570 2.4542 29.4661 2.9630 1.1498 26.7052 33.4180 25.7059 2.8102 7.2425 21.2268 17.4456 14.2501 27.5761 21.2414 9.4662 10.1100 26.7973 28.6504 70.1179 8.8241 36.0551 54.9792 36.2884 68.5725 28.7190 46.2376 14.9500 1.0163 39.3414 29.6502 30.5582 11.7111 67.0697 56.5206 72.8018 9.0065 7.6874 85.4536 36.5090 8.2195 53.3496 18.3610 77.2320 73.6575 2.0308 38.2263 3.3534 49.8553 41.8683 46.1469 7.2914 7.7287 83.6567 60.2600 15.5538 15.5343 44.4342 6.2360 42.7931 44.7329 8.4118 42.1157 23.1774 7.7284 23.2151 60.1743 57.1791 7.0009 40.0524 23.9284 0.2605 31.0685 30.5718 11.0160 13.3828 48.6971 25.2685 10.0978 17.8086 31.7947 71.5676 51.8351 58.0839 13.2866 15.7191 16.5029 18.5515 16.6898 56.5849 64.8185 62.0882 42.5435 9.6866 22.2981 60.7516 45.1036 0.7895 6.5887 12.1906 6.8386 14.4400 27.3043 10.2005 14.2154 6.7411 20.5707 27.1561 38.9482 28.7804 42.8425 0.6567 41.9575 20.8353 36.7381 13.0758 19.3125 42.4776 26.6287 42.7192 28.6724 17.9578 0.0907 19.8886 33.4197 42.7611 20.4742 22.5496 7.1592 28.0809 38.9569 44.6352 3.3170 34.5111 4.4571 40.6167 6.3872 40.0952 11.3050 29.9233 4.4415 25.9399 29.0448 8.8677 22.5077 5.5074 3.1903 25.4714 28.4310 14.6487 28.5560 9.8671 32.3807 31.4117 25.7809 8.5073 24.6330 6.3578 23.4629 21.4974 1.8109 22.4787 21.5183 16.5179 7.7907 7.8747 0.0473 20.7600 20.0000 3.8366 16.4467 15.4432 3.3084 18.9748 4.9038 2.3984 19.6398 4.3193 41.2900 2.7646 41.4771 10.2105 32.9969 23.3350 1.1755 36.5457 22.8772 0.1810 2.6274 22.9043 21.9301 38.8617 24.1917 24.9485 44.3494 17.4221 40.7823 43.1613
% Normalize rows of 'data' so each row sums to 1
dataNorm = data./sum(data,2)
dataNorm = 20×20
0.0133 0.0667 0.0422 0.0077 0.0924 0.0093 0.0036 0.0837 0.1048 0.0806 0.0088 0.0227 0.0665 0.0547 0.0447 0.0864 0.0666 0.0297 0.0317 0.0840 0.0399 0.0976 0.0123 0.0502 0.0765 0.0505 0.0954 0.0400 0.0643 0.0208 0.0014 0.0547 0.0413 0.0425 0.0163 0.0933 0.0786 0.1013 0.0125 0.0107 0.1112 0.0475 0.0107 0.0694 0.0239 0.1005 0.0958 0.0026 0.0497 0.0044 0.0649 0.0545 0.0600 0.0095 0.0101 0.1088 0.0784 0.0202 0.0202 0.0578 0.0114 0.0782 0.0818 0.0154 0.0770 0.0424 0.0141 0.0424 0.1100 0.1045 0.0128 0.0732 0.0437 0.0005 0.0568 0.0559 0.0201 0.0245 0.0890 0.0462 0.0147 0.0259 0.0463 0.1042 0.0755 0.0846 0.0194 0.0229 0.0240 0.0270 0.0243 0.0824 0.0944 0.0904 0.0620 0.0141 0.0325 0.0885 0.0657 0.0011 0.0153 0.0282 0.0158 0.0334 0.0632 0.0236 0.0329 0.0156 0.0476 0.0629 0.0902 0.0666 0.0992 0.0015 0.0972 0.0482 0.0851 0.0303 0.0447 0.0984 0.0529 0.0849 0.0570 0.0357 0.0002 0.0395 0.0664 0.0849 0.0407 0.0448 0.0142 0.0558 0.0774 0.0887 0.0066 0.0686 0.0089 0.0807 0.0127 0.0797 0.0300 0.0794 0.0118 0.0688 0.0771 0.0235 0.0597 0.0146 0.0085 0.0676 0.0755 0.0389 0.0758 0.0262 0.0859 0.0834 0.0684 0.0226 0.0654 0.0169 0.0927 0.0850 0.0072 0.0888 0.0850 0.0653 0.0308 0.0311 0.0002 0.0820 0.0790 0.0152 0.0650 0.0610 0.0131 0.0750 0.0194 0.0095 0.0776 0.0171 0.0836 0.0056 0.0840 0.0207 0.0668 0.0472 0.0024 0.0740 0.0463 0.0004 0.0053 0.0464 0.0444 0.0787 0.0490 0.0505 0.0898 0.0353 0.0825 0.0874
% 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.')

Community Treasure Hunt

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

Start Hunting!