Smart for loop for summing cell array matrices

1 vue (au cours des 30 derniers jours)
federico nutarelli
federico nutarelli le 7 Juil 2021
Hi all,
I have an empty cell test_set_class and a cell matrix class_test made of 100x30 matrices (i.e. each element of classs_test is a 1229x119 matrix). What I would like to do is to perform the following
N_RIPETIZIONI=100;
for RIPETIZIONE = 1:N_RIPETIZIONI
test_set_class{RIPETIZIONE}=class_test{RIPETIZIONE,1}+class_test{RIPETIZIONE,2}+class_test{RIPETIZIONE,3}+class_test{RIPETIZIONE,4}+...
class_test{RIPETIZIONE,5}+class_test{RIPETIZIONE,6}+class_test{RIPETIZIONE,7}+class_test{RIPETIZIONE,8}+class_test{RIPETIZIONE,9}+class_test{RIPETIZIONE,10}+...
class_test{RIPETIZIONE,11}+class_test{RIPETIZIONE,12}+class_test{RIPETIZIONE,13}+class_test{RIPETIZIONE,14}+...
class_test{RIPETIZIONE,15}+class_test{RIPETIZIONE,16}+class_test{RIPETIZIONE,17}+class_test{RIPETIZIONE,18}+class_test{RIPETIZIONE,19}+class_test{RIPETIZIONE,20}+...
class_test{RIPETIZIONE,21}+class_test{RIPETIZIONE,22}+class_test{RIPETIZIONE,23}+class_test{RIPETIZIONE,24}+...
class_test{RIPETIZIONE,25}+class_test{RIPETIZIONE,26}+class_test{RIPETIZIONE,27}+class_test{RIPETIZIONE,28}+class_test{RIPETIZIONE,29}+class_test{RIPETIZIONE,30};
end
but looping also on the rows. I tried something like:
for RIPETIZIONE = 1:N_RIPETIZIONI
for cols =2:N_cols
test_set_class{RIPETIZIONE}=class_test{RIPETIZIONE,1}+class_test{RIPETIZIONE,cols};
end
end
but I am not sure that it does what I want.
Any help please?

Réponse acceptée

Steven Lord
Steven Lord le 7 Juil 2021
a cell matrix class_test made of 100x30 matrices (i.e. each element of classs_test is a 1229x119 matrix)
In this case, where all the elements inside the cells of class_test are the same type and size, I would consider a different data structure.
c = cell(3, 4);
A = zeros(2, 5, 3, 4);
for row = 1:size(c, 1)
for col = 1:size(c, 2)
c{row, col} = rand(2, 5);
A(:, :, row, col) = c{row, col};
end
end
celldisp(c)
c{1,1} = 0.3306 0.6299 0.6771 0.9105 0.2659 0.1979 0.7490 0.9951 0.4654 0.7687 c{2,1} = 0.8961 0.4946 0.2456 0.7889 0.0785 0.1325 0.1188 0.3285 0.9613 0.8639 c{3,1} = 0.5348 0.0686 0.0419 0.2903 0.8682 0.8177 0.3323 0.1261 0.0416 0.5009 c{1,2} = 0.0622 0.6295 0.1948 0.7948 0.0402 0.0928 0.0580 0.8608 0.9771 0.7861 c{2,2} = 0.7431 0.1538 0.2800 0.8544 0.1884 0.3732 0.6756 0.6714 0.9956 0.1708 c{3,2} = 0.6587 0.9058 0.2324 0.9266 0.6938 0.0685 0.2263 0.9513 0.7382 0.4114 c{1,3} = 0.3168 0.6707 0.1110 0.2844 0.2740 0.2383 0.9724 0.8853 0.0172 0.1679 c{2,3} = 0.2090 0.4523 0.5057 0.4638 0.1971 0.8271 0.7511 0.1102 0.2951 0.3152 c{3,3} = 0.5004 0.7998 0.9737 0.7259 0.6527 0.4693 0.9873 0.5821 0.1988 0.9433 c{1,4} = 0.6058 0.4562 0.5678 0.8480 0.7427 0.4811 0.5137 0.6755 0.2705 0.6834 c{2,4} = 0.4182 0.0767 0.7408 0.0116 0.6074 0.6580 0.3813 0.8827 0.1208 0.7691 c{3,4} = 0.0462 0.5263 0.3671 0.2239 0.2728 0.7184 0.0575 0.6601 0.5686 0.4865
disp(A)
(:,:,1,1) = 0.3306 0.6299 0.6771 0.9105 0.2659 0.1979 0.7490 0.9951 0.4654 0.7687 (:,:,2,1) = 0.8961 0.4946 0.2456 0.7889 0.0785 0.1325 0.1188 0.3285 0.9613 0.8639 (:,:,3,1) = 0.5348 0.0686 0.0419 0.2903 0.8682 0.8177 0.3323 0.1261 0.0416 0.5009 (:,:,1,2) = 0.0622 0.6295 0.1948 0.7948 0.0402 0.0928 0.0580 0.8608 0.9771 0.7861 (:,:,2,2) = 0.7431 0.1538 0.2800 0.8544 0.1884 0.3732 0.6756 0.6714 0.9956 0.1708 (:,:,3,2) = 0.6587 0.9058 0.2324 0.9266 0.6938 0.0685 0.2263 0.9513 0.7382 0.4114 (:,:,1,3) = 0.3168 0.6707 0.1110 0.2844 0.2740 0.2383 0.9724 0.8853 0.0172 0.1679 (:,:,2,3) = 0.2090 0.4523 0.5057 0.4638 0.1971 0.8271 0.7511 0.1102 0.2951 0.3152 (:,:,3,3) = 0.5004 0.7998 0.9737 0.7259 0.6527 0.4693 0.9873 0.5821 0.1988 0.9433 (:,:,1,4) = 0.6058 0.4562 0.5678 0.8480 0.7427 0.4811 0.5137 0.6755 0.2705 0.6834 (:,:,2,4) = 0.4182 0.0767 0.7408 0.0116 0.6074 0.6580 0.3813 0.8827 0.1208 0.7691 (:,:,3,4) = 0.0462 0.5263 0.3671 0.2239 0.2728 0.7184 0.0575 0.6601 0.5686 0.4865
What does making a 4-dimensional array A get us? Compare how to sum up the matrices:
result1 = zeros(2, 5);
for k = 1:numel(c)
result1 = result1 + c{k};
end
result1
result1 = 2×5
5.3219 5.8641 4.9380 7.1229 4.8817 5.0749 5.8233 7.7292 5.6502 6.8673
result2 = sum(A, [3 4]) % sum over dimensions 3 and 4
result2 = 2×5
5.3219 5.8641 4.9380 7.1229 4.8817 5.0749 5.8233 7.7292 5.6502 6.8673

Plus de réponses (1)

Jan
Jan le 7 Juil 2021
Yes, this does, what you expect. Simply compare the results of both methods.
This is faster and looks cleaner:
for RIPETIZIONE = 1:N_RIPETIZIONI
S = 0;
for cols =2:N_cols
S = S + class_test{RIPETIZIONE, cols};
end
test_set_class{RIPETIZIONE} = S;
end

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by