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일

0 개 추천

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일

1 개 추천

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일

1 개 추천

Many thanks Kalyan Acharja and Rik. Much appreciated.
+

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by