distributing two values in 3d array
Show older comments
I want to distribute two chosen values in 3d array. I have a below code I have created. How can I distribute only two values instead of rand() function? Much appreciate your guidance.
L=60; % Number of columns
b=25; % Number of rows
w=25; %the depth of the 3D lattice
K_f1=5e6;
K_f2=2e6;
b_centre=3;
b_sides=(b-b_centre)/2;
b_1=1:b_sides;
b_2=b_sides+1:b_sides+b_centre;
b_3=b_sides+b_centre+1:b;
A=zeros(L,b,w);
for r=1:L
for q=1:b_sides
for s=1:w
A(r,q,s)=K_f1*rand;
end
end
end
for r=1:L
for q=b_sides+1:b_sides+b_centre
for s=1:w
A(r,q,s)=K_f2*rand;
end
end
end
for r=1:L
for q=b_sides+b_centre+1:b
for s=1:w
A(r,q,s)=K_f1*rand;
end
end
end
Accepted Answer
More Answers (2)
Rik
on 11 Mar 2019
You could also do the following:
L=60; % Number of columns
b=25; % Number of rows
w=25; %the depth of the 3D lattice
K_f1=5e6;
K_f2=2e6;
list_of_values=[K_f1 K_f2];
ind=randi(numel(list_of_values),b,L,w);%generate indexing matrix
output=list_of_values(ind);%apply the random choices
This syntax allows you to expand beyond two values and will not require you to make assumptions about the characteristics of the filler values.
Girvani Manoharan
on 12 Mar 2019
1 vote
1 Comment
KALYAN ACHARJYA
on 12 Mar 2019
Edited: KALYAN ACHARJYA
on 12 Mar 2019
Welcome Always
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!