How to create a big 3D matrix from different blocks with the same volume?

2 views (last 30 days)
SAC CSA
SAC CSA on 5 Jan 2022
Edited: Kevin Holly on 6 Jan 2022
I would like to generate a 3D matrix of dimension n1xn1xn1 compose of blocks with dimension n2*n2*n2, where all elements of each block have the same value, which can be a, b, c or d. The position of each block with its elements is randomly allocated in the main matrix (n1*n1*n1). Attached figure outlines the proposal.
Taking into account that the sum of all blocks does not exactly correspond to the volume of the main matrix (n1*n1*n1) and the difficulty in adjusting the blocks in the main matrix, null elements can be used to complete the elements in the main matrix.
n1=100; % dimension of main matrix
n2=16; % dimension of blocks
m=zeros(n1,n1,n1); %Null matrix of dimension 100x100x100
m1=ones(n2,n2,n2); %Unitary matrix of dimension 16x16x16
a=5; b=4; c=3; d=2;
m2=a*m1;
m3=b*m1;
m4=c*m1;
m5=d*m1;
How to randomly allocate m2, m3, m4 and m5 in about 216 blocks of m?
Can anybody help me?
Thank you so much in advance.
  2 Comments
SAC CSA
SAC CSA on 5 Jan 2022
Hi @Jan, thank you for the comment. Indeed, I'm trying to allocate 216 blocks of matrices (54 of each matrix labeled by m2, m3, m4 and m5) in random orders in matrix m. The 216 blocks of 16*16*16 matrices result in 884,736 elements. The other 115,264 elements to complete 1,000,000 elements of the m matrix can be zeros.

Sign in to comment.

Answers (1)

Kevin Holly
Kevin Holly on 6 Jan 2022
Edited: Kevin Holly on 6 Jan 2022
n1=100; % dimension of main matrix
n2=16; % dimension of blocks
m=zeros(n1,n1,n1); %Null matrix of dimension 100x100x100
m1=ones(n2,n2,n2); %Unitary matrix of dimension 16x16x16
coeff = [5 4 3 2];
blocks = (floor(n1/n2))^3;
list = 1:blocks;
key = reshape(list,floor(n1/n2),floor(n1/n2),floor(n1/n2));
for i = randperm(blocks)
[xpos, ypos, zpos] = ind2sub(size(key),find(key==i));
m(xpos*n2-(n2-1):xpos*n2,ypos*n2-(n2-1):ypos*n2,zpos*n2-(n2-1):zpos*n2)=coeff(randi(4))*m1;
end
m2 = m==2;
m3 = m==3;
m4 = m==4;
m5 = m==5;
Verfiy
figure
s = isosurface(m2);
p = patch(s);
set(p,'FaceColor',[1 0 0]);
set(p,'EdgeColor','none');
hold on
s = isosurface(m3);
p = patch(s);
set(p,'FaceColor',[0 1 0]);
set(p,'EdgeColor','none');
s = isosurface(m4);
p = patch(s);
set(p,'FaceColor',[0 0 1]);
set(p,'EdgeColor','none');
s = isosurface(m5);
p = patch(s);
set(p,'FaceColor',[1 0 1]);
set(p,'EdgeColor','none');
view(127,45)
axis equal
xlim([0 100])
ylim([0 100])
zlim([0 100])

Community Treasure Hunt

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

Start Hunting!