distributing two values in 3d array

1 view (last 30 days)
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

KALYAN ACHARJYA
KALYAN ACHARJYA on 11 Mar 2019
Edited: KALYAN ACHARJYA on 11 Mar 2019
How can I distribute only two values instead of rand() function?
You can fixed the value as per your need
randi([1, 2],1)/10 ;
Please replace the value 1 or 2, as per your desired value
This expression pick any one value in between 0.1 or 0.2 (Either 0.1 or 0.2)

More Answers (2)

Rik
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
Girvani Manoharan on 12 Mar 2019
Many thanks Kalyan Acharja and Rik. Much appreciated.
+

Categories

Find more on Random Number Generation 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!