how to average a vector of arrays in Matlab

조회 수: 8 (최근 30일)
Abdullah Almohammadi
Abdullah Almohammadi 2022년 6월 27일
답변: Steven Lord 2022년 6월 27일
Hello,
im new to matlab and I'd like to average a vector of arrays in matlab?
i have a vector of size (1,72) each column has an array of size(22,22)
i wanna avgerge the vector to get a desired output of (22,22)
i have tried this piece of code
for i= 1:72
wholeMean1{1} = mean(indexes_every.PLV.data {1,i}, 1); %% the output (1,22)
wholeMean2{1} = mean(indexes_every.PLV.data {1,i}, 2); %% the output (22,1)
wholeMean3{1} = mean(indexes_every.PLV.data {1,i}, 3); %% the output (22,22) ##
end
i just want to verfiy my result.
am i doing it correctly ?
thx in advanced

답변 (2개)

Image Analyst
Image Analyst 2022년 6월 27일
One simple intuitive way is to just add up all the arrays and divide by the number of the arrays
numArrays = numel(indexes_every.PLV.data)
sumMatrix = zeros(22, 22);
for k = 1 : numArrays
sumMatrix = sumMatrix + indexes_every.PLV.data{k};
end
meanMatrix = sumMatrix / numArrays

Steven Lord
Steven Lord 2022년 6월 27일
Concatenate them in the third dimension then call mean with the dimension input.
M = magic(4)
M = 4×4
16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1
E = eye(4)
E = 4×4
1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
N = ones(4)
N = 4×4
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
C = {M, E, N}
C = 1×3 cell array
{4×4 double} {4×4 double} {4×4 double}
A = cat(3, C{:})
A =
A(:,:,1) = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 A(:,:,2) = 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 A(:,:,3) = 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
mean(A, 3)
ans = 4×4
6.0000 1.0000 1.3333 4.6667 2.0000 4.3333 3.6667 3.0000 3.3333 2.6667 2.6667 4.3333 1.6667 5.0000 5.3333 1.0000

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by