I want to divide an array into x number of smaller arrays of size [m x n] (where x is = m *n) and then using a for loop fill those smaller arrays with the same value

1 次查看(过去 30 天)
I have an array that is 6 x 8, I want a user to be able to input 2 values, a # of rows (m) and a # of columns (n), ex x = zeros(2,3) and then the larger array is divided into m*n sections each of size [m,n] and in a for loop for 1:m*n the user can input values for x and those values are then inplemented into the corresponding section of the larger array. I want to keep the large array all as one, I just need an easy way to input the same value into multiple cells at once.
rows = 8;
columns = 6;
layout = zeros(rows,columns);
%n = input(['How many horizontal replicates are there']); ex.
%m = input(['How many vertical replicates are there']); ex.
n = 3;
m = 2;
sections = m*n;
%need to now divide 'layout' into sections which each have a size of [m,n]
for i = 1:sections
%x = input(['What is the value in section ' num2str(i), '?' ]); ex.
%the value of x is now filled into the [m x n] array of the i section
x = i+1;
% layout [???] = x;
end
Thanks!

回答(2 个)

Stephen23
Stephen23 2024-12-19
移动:Matt J 2024-12-24
Changing your perspective on this problem would make it easier.
A much simpler approach would be to use REPELEM:
U = [3,9,6;2,7,9] % user values (if required use a simple loop to enter the values)
U = 2×3
3 9 6 2 7 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
S = [2,3] % size of each tile
S = 1×2
2 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
M = repelem(U,S(1),S(2))
M = 4×9
3 3 3 9 9 9 6 6 6 3 3 3 9 9 9 6 6 6 2 2 2 7 7 7 9 9 9 2 2 2 7 7 7 9 9 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sahas
Sahas 2024-12-20
Hi @Grace,
As per my understanding, it seems there are two tasks at hand. First, you want to split a larger array into m*n number of smaller arrays, each with dimensions m*n as well. Second, you aim to iterate over these sections in the larger array and populate them with the corresponding values from the smaller arrays.
Refer to the following code snippet which takes a different approach and eventually gets the requirements and takes in input from the user as well:
% Define the dimensions of the layout
rows = 8;
columns = 6;
layout = zeros(rows, columns);
% Prompt the user for the number of vertical and horizontal replicates
m = input('Number of rows in the section: ');
n = input('Number of cols in the section: ');
sections = m*n;
% Initialize the small_array to store user inputs
small_array = zeros(1, sections);
for i = 1:sections
small_array(i) = input(['Enter value for element ' num2str(i) ': ']);
end
% Creating sections
section_array = reshape(small_array, m, n);
disp(section_array)
% Use repelem to create a repeated pattern
C = repelem(section_array', m, n);
disp(C)
% Overlap C on the layout, if required
layout(1:size(C, 1), 1:size(C, 2)) = C;
disp(layout);
For more information on MATLAB's "repelem" function, refer to the following MathWorks documentation link:
Hope this is beneficial!

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by