Summing every nth column

조회 수: 39 (최근 30일)
Adnan Habib
Adnan Habib 2021년 11월 29일
댓글: Adnan Habib 2021년 11월 29일
Hi,
I have a 1336 by 89700 matrix. I want to sum every 300th colum starting from the first column so that I end up with a 1336 by 300 matrix. So the first colum in the new matrix will be the sum of colum 1, 300, 599....etc. of the previous matrix. And the second column of the new matrix will be the sum of column 2, 301, 600...etc. of the previous matrix. I hope I was able to clearly explain what I want. Any help will be appreciated.

채택된 답변

Image Analyst
Image Analyst 2021년 11월 29일
편집: Image Analyst 2021년 11월 29일
Try this and see if it does what you want:
data = rand(1336, 89700); % Create sample data.
sums = zeros(1336, 299); % Preallocate.
for col = 1 : size(data, 2) / 299
% Extract every 300th column, starting with column "col", into a new matrix.
extractedMatrix = data(:, col : 300 : end); % A new matrix 1/300th as wide as the original
% Sum all the values in that matrix, and put that sum into means.
% Get the sums for just this one set of columns.
theseSums = sum(extractedMatrix, 2);
% Store these sums in the column "col" of our master array.
sums(:, col) = theseSums; % Get a single sum from the entire 2-D matrix.
end
whos sums
Name Size Bytes Class Attributes sums 1336x300 3206400 double
  댓글 수: 1
Adnan Habib
Adnan Habib 2021년 11월 29일
This worked! Thank you so much

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

추가 답변 (2개)

Yongjian Feng
Yongjian Feng 2021년 11월 29일
Your logic is not consistent. from 1 to 300, there are 298 rows in between. But from 300 to 600, there are 299 rows in between.
You might want 1, 301, 601, etc?
You can use reshape and sum to do this. Here a simpler example of summing 1, 4th, 7th.
ma = [1 2 3; 4 5 6; 7 8 9; 10 11 12; 13 14 15; 16 17 18;19 20 21; 22 23 24; 25 26 27]
mb = reshape(ma', 9, 3)
mc = sum(mb, 2)
me = reshape(mc, 3, 3)'
ma =
1 2 3
4 5 6
7 8 9
10 11 12
13 14 15
16 17 18
19 20 21
22 23 24
25 26 27
mb =
1 10 19
2 11 20
3 12 21
4 13 22
5 14 23
6 15 24
7 16 25
8 17 26
9 18 27
mc =
30
33
36
39
42
45
48
51
54
me =
30 33 36
39 42 45
48 51 54
  댓글 수: 1
Adnan Habib
Adnan Habib 2021년 11월 29일
Thanks for this. Yes I made a mistake. It should be 1+300+599....i.e. 298 columns in between.

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


Image Analyst
Image Analyst 2021년 11월 29일
I think this does what you said, though I'm not sure it's what you want:
data = rand(1336, 89700); % Create sample data.
means = zeros(1, 299); % Preallocate.
for col = 1 : 299
% Extract every 300th column, starting with column "col", into a new matrix.
extractedMatrix = data(:, col : 300 : end); % A new matrix 1/300th as wide as the original
% Sum all the values in that matrix, and put that sum into means.
means(col) = sum(extractedMatrix(:)); % Get a single sum from the entire 2-D matrix.
end
  댓글 수: 2
Adnan Habib
Adnan Habib 2021년 11월 29일
Hi thanks for your answer. But this is not what I wanted. With this my output matrix is 1 by 299. But I want my output matrix to be 1336 by 300
Image Analyst
Image Analyst 2021년 11월 29일
OK, let's examine this. So your original matrix is 1336 rows tall by 89700 columns wide. So if you take every 299'th column (1, 300, 599, etc.) you get an extracted matrix of 1336 rows tall by 299 columns wide.
So now you say "So the first colum in the new matrix will be the sum of colum 1, 300, 599....etc. of the previous matrix." so we're to sum the extracted matrix row-by-row over all the columns so we'd end up with a new matrix of 1336 by 1 for the sum over the columns. So we'd do sums = sum(extractedMatrix, 2).
See my other answer elsewhere on the page for a full solution.

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

카테고리

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