Mean of every n number of doubles in a cell

조회 수: 2 (최근 30일)
Adnan Habib
Adnan Habib 2023년 2월 28일
댓글: Adnan Habib 2023년 2월 28일
I have a cell with 990 doubles of 300 by 300 matrices. Lets call it T
I want to create a new cell with 26 doubles of 300 by 300 matrices each of which are the mean values of every 39 doubles from T (the 26th double of the new cell will be the mean of the final 15 doubles of T). i.e. (39 X 25) + (15 X 1) = 990.
Kindly help me with the code for this.
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 2월 28일
How do you want to group them?
#1 - [1-39], [40-78], [79-117], ...
#2 - [1,39,78,117, ...], [2, 40, 79, 118, ...], [3, 41, 80, 119, ...]
Adnan Habib
Adnan Habib 2023년 2월 28일
Hi Dyuman Joshi, I want the first one 1-39, 40-78 etc.

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

채택된 답변

Jan
Jan 2023년 2월 28일
편집: Jan 2023년 2월 28일
I've reduced the test data size to 30x30 matrices - use the original sizes for your implementation:
T = squeeze(num2cell(rand(30, 30, 990), 1:2)); % Some test data, {990 x 1} cell
nT = numel(T);
R = cell(26, 1);
iR = 0;
for iT = 1:39:nT % Initial index of this block
fT = min(nT, iT + 38); % Final index of this block
tmp = 0;
for k = iT:fT % Accumulate elements of T
tmp = tmp + T{k};
end
iR = iR + 1; % Next output
R{iR} = tmp / (fT - iT + 1); % Mean value
end
Alternative approach:
nT = numel(T);
% Create [1, 40, 79, ...] with the last element is nT+1:
w = 39;
iT = 1:w:nT;
iT(end + (iT(end) == nT)) = nT + 1; % Consider nT is divisable by w
nR = numel(iT) - 1;
R2 = cell(nR, 1);
for iR = 1:nR
fT = iT(iR + 1) - 1; % Final index of this block
tmp = 0;
for k = iT(iR):fT % Accumulate elements of T
tmp = tmp + T{k};
end
R2{iR} = tmp / (fT - iT(iR) + 1); % Mean value
end
  댓글 수: 1
Adnan Habib
Adnan Habib 2023년 2월 28일
Thanks a lot Jan. The first one works. I didn't even try the alternative approach after that.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by