Splitting arrays using loops
1 回表示 (過去 30 日間)
古いコメントを表示
would there be a way to create a loop for splitting an 8x8 array into seperate 4x4 arrays and then find the mean of each of the 4x4 arrays and place each of the mean values into a new array ?
5 件のコメント
Stephen23
2023 年 8 月 11 日
"No need for permute, squeeze does the job just fine"
Not really, because SQUEEZE is fragile, unlike PERMUTE. It all looks "fine" ... until one day the user has data which consists of one row of blocks and are then astonished when the output has the wrong orientation:
A = reshape(1:32,4,8)
M = squeeze(mean(reshape(A,[4 1 4 2]),[1 3])) % oops, wrong output
M = mean(permute(reshape(A,4,1,4,2),[2,4,1,3]),3:4) % aaah, much better
M = permute(mean(reshape(A,4,1,4,2),[1,3]),[2,4,1,3]) % also this
SQUEEZE is just like LENGTH: used only by people who like hidden bugs in their code.
Bruno Luong
2023 年 8 月 11 日
編集済み: Bruno Luong
2023 年 8 月 11 日
OP stated clearly he wants average on 8 x 8 matrix not 4 x 8.
But granted if one doesn't like squeeze, in this block average problem use reshape rather than permute for efficienty
A = reshape(1:32,4,8)
M = reshape(mean(reshape(A,[4 1 4 2]),[1 3]), [1 2])
採用された回答
C B
2023 年 8 月 10 日
I have already answered you same apart from making mean value as new array. here is how you can do it.
A = reshape(1:64, [8,8]);
mean_values = zeros(2,2);
% Loop to extract 4x4 matrices and compute mean
for row = 1:2
for col = 1:2
r_idx = (row-1)*4 + 1 : row*4;
c_idx = (col-1)*4 + 1 : col*4;
submatrix = A(r_idx, c_idx);
mean_values(row, col) = mean(submatrix(:));
end
end
disp(mean_values);
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!