how to split an 8x8 2D array
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Freja
am 10 Aug. 2023
Kommentiert: Chetan Bhavsar
am 10 Aug. 2023
how would I split an 8x8 2D array into four 4x4 arrays so that I can take the mean of each 4x4 array using the mean function?
0 Kommentare
Akzeptierte Antwort
Chetan Bhavsar
am 10 Aug. 2023
Bearbeitet: Chetan Bhavsar
am 10 Aug. 2023
Is this what you are looking for?
% Sample 8x8 array
A = reshape(1:64, [8,8])
% Splitting into 4x4 arrays
A1 = A(1:4, 1:4)
A2 = A(1:4, 5:8)
A3 = A(5:8, 1:4)
A4 = A(5:8, 5:8)
% Computing the mean for each 4x4 array
mean_A1 = mean(A1(:));
mean_A2 = mean(A2(:));
mean_A3 = mean(A3(:));
mean_A4 = mean(A4(:));
% Displaying the results
fprintf('Mean of A1: %f\n', mean_A1);
fprintf('Mean of A2: %f\n', mean_A2);
fprintf('Mean of A3: %f\n', mean_A3);
fprintf('Mean of A4: %f\n', mean_A4);
2 Kommentare
Chetan Bhavsar
am 10 Aug. 2023
Yes we can use loop for splitting and Mean calculation
A = reshape(1:64, [8,8]);
mean_values = zeros(1, 4);
% Loop to extract 4x4 matrices and compute mean
for row = 1:2
for col = 1:2
r = (row-1)*4 + 1 : row*4;
c = (col-1)*4 + 1 : col*4;
submatrix = A(r, c);
idx = (row-1)*2 + col;
mean_values(idx) = mean(submatrix(:));
end
end
for i = 1:4
fprintf('Mean of A%d: %f\n', i, mean_values(i));
end
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Resizing and Reshaping Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!