Divide each element in a column by the sum of that column

조회 수: 28 (최근 30일)
Leeba Chacko
Leeba Chacko 2021년 11월 4일
댓글: Stephen23 2021년 11월 4일
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?

답변 (1개)

Sreedevi K
Sreedevi K 2021년 11월 4일
iwant = A./sum(A)
  댓글 수: 3
Leeba Chacko
Leeba Chacko 2021년 11월 4일
I closed and reopened MATLAB and it seems to be working now. Thanks!
Stephen23
Stephen23 2021년 11월 4일
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)
B = 2×10
0.3429 0.5000 0.5076 0.4182 0.5000 0.3909 0.4182 0.3382 0.2072 0.4242 0.6571 0.5000 0.4924 0.5818 0.5000 0.6091 0.5818 0.6618 0.7928 0.5758

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by