distributing two values in 3d array
이전 댓글 표시
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
채택된 답변
추가 답변 (2개)
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
2019년 3월 12일
1 개 추천
댓글 수: 1
KALYAN ACHARJYA
2019년 3월 12일
편집: KALYAN ACHARJYA
2019년 3월 12일
Welcome Always
카테고리
도움말 센터 및 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!