distributing two values in 3d array

1 次查看(过去 30 天)
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

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-3-11
编辑:KALYAN ACHARJYA 2019-3-11
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)

更多回答(2 个)

Rik
Rik 2019-3-11
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 2019-3-12
Many thanks Kalyan Acharja and Rik. Much appreciated.
+

类别

Help CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by