Sum: every nth column and groups/blocks of columns

조회 수: 11 (최근 30일)
Dom
Dom 2022년 8월 24일
편집: Dyuman Joshi 2022년 8월 24일
Hi:
Is there any way to improve this code?
Thanks
Dominic
%
% Flows between sectors of countries
% 3 countries with 4 sectors
% Row and column headings are the same C1S1 C1S2 ......C4S3 C4S4
%
c=3; % no. of countries
s=4; % no. of sectors
X=magic(c*s); % 12
%
% add every 4th (sth) column of a matrix: 1,5,9 ... 4,8,12
% - result 12x4 (sum by sector)
%
S=sum(X(:,1:s:end),2); % 1:4:end
for k=2:s % 2:4
SS=sum(X(:,(k):s:end),2);
S=horzcat(S,SS);
end
%
% add columns in blocks of 4: 1-4, 5-8, 9-12 - result 12x3 (sum by country)
%
C=sum(X(:,[1:s]),2); % [1:4]
x=(s+1):s:(c*s); % 5:4:12
y=(2*s):s:(c*s); % 8:4:12
for k=[x;y]
CC=sum(X(:,[k(1):k(2)]),2);
C=horzcat(C,CC);
end

채택된 답변

Bruno Luong
Bruno Luong 2022년 8월 24일
편집: Bruno Luong 2022년 8월 24일
c=3; % no. of countries
s=4; % no. of sectors
X=magic(c*s); % 12
XX = reshape(X,size(X,1),s,[]);
S = sum(XX,3)
S = 12×4
420 18 21 411 51 381 378 60 87 345 342 96 312 126 129 303 276 162 165 267 195 237 234 204 231 201 198 240 168 270 273 159 132 306 309 123 339 93 90 348
C = squeeze(sum(XX,2))
C = 12×3
290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290
  댓글 수: 3
Bruno Luong
Bruno Luong 2022년 8월 24일
Not only fewer line of code. It probably the fatest as it avoid matrix transposition.
Dom
Dom 2022년 8월 24일
Thanks Bruno. I am sure it is.

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

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2022년 8월 24일
x=magic(12);
y=reshape(x,12,4,[]);
z=squeeze(sum(y,2))
z = 12×3
290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290
  댓글 수: 1
Dom
Dom 2022년 8월 24일
Thank you Jiang, very elegant. Any comments on the first part?
Thanks again for taking the time.

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


Dyuman Joshi
Dyuman Joshi 2022년 8월 24일
편집: Dyuman Joshi 2022년 8월 24일
%comment lines removed
c=3; % no. of countries
s=4; % no. of sectors
X=magic(c*s); % 12
Vectorized solution
S1=reshape(sum(reshape(X,[],c),2),[],s)
S1 = 12×4
420 18 21 411 51 381 378 60 87 345 342 96 312 126 129 303 276 162 165 267 195 237 234 204 231 201 198 240 168 270 273 159 132 306 309 123 339 93 90 348
C1=reshape(sum(reshape(X',s,[])),[],c)
C1 = 12×3
290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290 290
  댓글 수: 3
Dyuman Joshi
Dyuman Joshi 2022년 8월 24일
I would suggest you to take a look at @Bruno Luong's answer as well, it's a bit simpler than mine.
I got to know about squeeze from the other 2 answers now only!
Dom
Dom 2022년 8월 24일
Thanks Dyuman. I have looked at all three contributions. @Bruno Luong and @Fangjun Jiang add another dimension. I am stuck in old loop and 2x2 thinking. All the answers work so I wonder why I can't accept all of them. Thanks.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by