how to take average of each column of matrices

11 ビュー (過去 30 日間)
chan
chan 2021 年 11 月 30 日
コメント済み: Stephen23 2021 年 12 月 3 日
A = randi(9,[6 3 6]);
U = 3:5;
% do it with a loop
M = zeros(size(A,1),size(A,2),numel(U));
for k = 1:length(U)
M(:,:,k) = A(U(k),1,2) * A(:,:,U(k));
end
%Here i have 3 matrices i.e. M(:,:,1),M(:,:.2),M(:,:,3).
Could someone suggest me a hint on how to take the average of each column of the three matrices. result will be a 6*3 matrix
"sendcnt" can be 3/4/5
when i do M/sendcnt then each cell of the matrix is divided by sendcnt which is not right.
what i want is (1st column of 1st matrix+1st column of 2nd matrix+1st column of 3rd matrix)/sendcnt
similarly for the other 2nd and 3rd column.
  1 件のコメント
Stephen23
Stephen23 2021 年 12 月 3 日
"i want mean for column of the 3D matrix. it will be 6*3 matrix"
The mean along column of a 6x3x6 array will return a 6x1x6 array.
I suspect that you might want the mean along the third dimension (aka "page"):
The mean along each "page" will return a 6x3(x1) array.

サインインしてコメントする。

回答 (2 件)

Rik
Rik 2021 年 11 月 30 日
It sounds like you want something like this:
A = randi(9,[6 3 6]);
U = 3:5;
% do it with a loop
M = zeros(size(A,1),size(A,2),numel(U));
for k = 1:length(U)
M(:,:,k) = A(U(k),1,2) * A(:,:,U(k));
end
data=mean(M,2); %apply mean over the second dimension (columns)
squeeze(data) % reshape the nx1xm to nxm
ans = 6×3
45.3333 25.0000 4.3333 18.6667 13.3333 4.3333 21.3333 16.6667 4.0000 58.6667 26.6667 4.6667 40.0000 26.6667 6.0000 18.6667 18.3333 5.0000
  3 件のコメント
chan
chan 2021 年 12 月 3 日
i want mean for column of the 3D matrix. it will be 6*3 matrix
Rik
Rik 2021 年 12 月 3 日
That is what mean(M,2) does, however, the result is not 6*3 but 6*1*3. The reason is that you only have 1 column left if you calculate the average of the columns.
Can you give an example of input and expected output?

サインインしてコメントする。


KSSV
KSSV 2021 年 11 月 30 日
A = randi(9,[6 3 6]);
iwant = mean(A,1)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by