How to sum by 3d array?

조회 수: 2 (최근 30일)
Tiina
Tiina 2016년 1월 6일
댓글: Andrei Bobrov 2016년 1월 8일
Hi,
I have the following sample columns structured in a 3D array of 2 pages (:,:,1) and (:,:,2). I want to produce out1 as a sum of 2 rows as indicated by s over page 1. That is, s is 2 sum 2 rows of data(this occurs on page 1 as indicated by idx1, if s is 3 sum 3 rows of data... idx1 is an index of each page that rolls over all rows, idx2 is the the row number. I cannot use permute/reshape as the rows in each page changes significantly in the actual data set. Could you please advise? Thanks
idx1=[1 1 1 1 2 2 2 2 2]';
idx2=[ 1 2 3 4 1 2 3 4 5]';
s=[2 2 2 2 3 3 3 3 3]';
data=[10 11 9 13 2 4 3 1 3]';
out1= [nan 21 20 22 nan nan 9 8 7]';
out2=[nan 2 3 4 nan nan 3 4 5]'

채택된 답변

Andrei Bobrov
Andrei Bobrov 2016년 1월 6일
편집: Andrei Bobrov 2016년 1월 6일
out1 = cell2mat(accumarray(idx1,(1:numel(data))',[],...
@(x){[nan(s(x(1))-1,1);conv2(data(x),ones(s(x(1)),1),'valid')]}));
out2 = idx2;
out2(isnan(out1))=nan;
  댓글 수: 2
Tiina
Tiina 2016년 1월 8일
Thanks Andrei, just a quick question if i dont have idx2, how do i recover it from a 3d array if i had idx1, s and data?
Andrei Bobrov
Andrei Bobrov 2016년 1월 8일
[~,~,c] = unique(idx1);
idx2 = cell2mat(accumarray(c,1,[],@(x){(1:numel(x))'}));

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by