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
37 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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!
0 Commenti
Risposte (2)
Stephen23
il 19 Dic 2024 alle 17:20
Spostato: Matt J
il 24 Dic 2024 alle 3:39
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)
S = [2,3] % size of each tile
M = repelem(U,S(1),S(2))
0 Commenti
Sahas
il 20 Dic 2024 alle 6:23
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!
0 Commenti
Vedere anche
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!