For loop to create a new matrix taking a cluster of columns from existing matrix

2 visualizzazioni (ultimi 30 giorni)
I have a matrix
FR_mat = randi(([1 20],1300,36);) % size 1300 rows, 36 columns
I would like to index this matrix FR_mat and and create a new matrix FR_sum with the outcome variables of my calculations.
I would like to take every 4 columns of the existing matrix and add it to each other so that 4 columns become one, with the values being the sum of all 4 columns. To visualize it, the following code is what I want to calculate. But I would like to do it in a For loop so that I have a shorter code and less work.
FR_sum(:,1) = FR_mat(:,1) + FR_mat(:,2) + FR_mat(:,3) + FR_mat(:,4);
FR_sum(:,2) = FR_mat(:,5) + FR_mat(:,6) + FR_mat(:,7) + FR_mat(:,8);
etc.
FR_sum(:,9) = FR_mat(:,33) + FR_mat(:,34) + FR_mat(:,35) + FR_mat(:,36);
This is what I tried so far:
k = 1;
for i = 1:11
i = i + 4
for j = 1:11
FR_sum(:,k) = sum(FR_mat(:,i:j);
k = k + 1 ;
end
end
Any ideas how I can manage this code?
Thank you for your help!

Risposta accettata

DGM
DGM il 4 Giu 2021
Modificato: DGM il 4 Giu 2021
Blockwise sum along dim 2 using a cell array
s = [1300 36]; % set the array size
FR_mat = randi([1 20],s(1),s(2));
C = mat2cell(FR_mat,s(1),ones(9,1)*4);
f = @(x) sum(x,2);
FR_sum = cell2mat(cellfun(f,C,'uniformoutput',false))
FR_sum = 1300×9
40 26 46 51 59 35 35 30 9 45 40 38 48 60 19 24 32 48 59 40 41 42 30 36 46 30 48 59 44 58 48 39 35 27 53 40 43 68 23 35 36 31 38 40 61 42 43 37 37 57 37 33 40 38 35 37 36 57 48 27 44 55 19 37 45 36 34 70 38 42 39 34 35 63 57 65 51 49 29 23 51 22 35 34 44 25 40 48 44 49
If you really want a loop version:
FR_sum2 = zeros(s(1),9);
for b = 1:9
FR_sum2(:,b) = sum(FR_mat(:,(1:4)+(b-1)*4),2);
end
FR_sum2
FR_sum2 = 1300×9
40 26 46 51 59 35 35 30 9 45 40 38 48 60 19 24 32 48 59 40 41 42 30 36 46 30 48 59 44 58 48 39 35 27 53 40 43 68 23 35 36 31 38 40 61 42 43 37 37 57 37 33 40 38 35 37 36 57 48 27 44 55 19 37 45 36 34 70 38 42 39 34 35 63 57 65 51 49 29 23 51 22 35 34 44 25 40 48 44 49

Più risposte (1)

Stephen23
Stephen23 il 4 Giu 2021
Modificato: Stephen23 il 4 Giu 2021
The MATLAB approach using reshape, permute, and sum:
inp = randi([1,20],1300,36)
inp = 1300×36
19 13 20 8 7 1 4 14 1 3 1 2 2 1 17 9 14 13 10 18 7 10 16 12 9 9 12 14 19 2 20 1 12 14 19 7 10 6 11 13 5 12 7 10 8 5 14 13 5 9 15 9 4 7 4 17 17 15 16 12 2 3 14 9 3 7 6 11 20 20 3 4 1 16 15 14 12 18 12 15 8 12 6 16 2 8 6 8 7 20 7 14 9 14 5 20 14 20 3 13 9 3 14 4 7 19 5 7 18 11 15 4 11 10 11 11 17 9 13 18 12 17 14 12 7 1 7 14 16 9 10 9 15 5 15 1 9 1 8 16 11 6 13 2 16 3 15 15 2 5 2 16 17 16 13 19 17 3 19 16 15 8 10 3 2 10 5 7 9 13 5 1 2 7 3 5 2 14 18 16 17 5 1 6 16 15 14 16 8 1 5 19 2 18 2 16 7 5 9 1 19 4 9 4 10 19 15 8 4 18 1 19 12 4 10 11 4 11 11 2 1 14 11 6 17 4 14 5 1 8 5 18 3 15 19 19 8 10 3 2 18 14 2 18 14 9 3 11 15 19 2 20 6 2 12 20 2 11 2 4 2 13 18 10 1 14 12 16 17 2 15 6 8 4 1 9 1 7 5 5 1 3 18 9 11 16 1 14 1 15 13 5 2 18 14 11 12 10 1 7
out = sum(permute(reshape(inp,1300,4,[]),[1,3,2]),3)
out = 1300×9
60 26 7 29 55 45 44 33 30 47 42 41 30 41 35 53 55 50 28 27 47 46 57 42 24 34 35 44 59 28 44 41 40 48 51 51 55 29 44 36 34 32 49 16 47 51 52 58 25 34 15 24 50 34 29 61 33 38 22 36 52 42 24 36 36 28 38 28 41 56 15 42 52 37 56 40 19 43 43 51 57 33 18 14 54 31 38 47 43 53
Checking the first four columns:
sum(inp(:,1:4),2)
ans = 1300×1
60 47 28 44 55 51 29 36 52 33

Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by