Divide each element in a column by the sum of that column
14 views (last 30 days)
Show older comments
I have a 2x10 matrix called A. I would like to find the sum of each column in A and divide individual elements in A by the sum for each column.
ex. A = [12 45 67 23 45 43 23 23 23 56
23 45 65 32 45 67 32 45 88 76];
B = sum(A) i.e [ 35 90 132 55 90 110 55 68 111 132];
Next, I want to divde 12 & 23 by 35, 45 and 45 by 90 and so on. How do I do this?
0 Comments
Answers (1)
Sreedevi K
on 4 Nov 2021
iwant = A./sum(A)
3 Comments
Stephen23
on 4 Nov 2021
Specify the dimension argument to avoid bugs if the matrix happens to contain just one column:
A = [12,45,67,23,45,43,23,23,23,56;23,45,65,32,45,67,32,45,88,76];
B = A./sum(A,1)
See Also
Categories
Find more on Creating and Concatenating Matrices 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!