Programmatically define elements of a matrix

조회 수: 2 (최근 30일)
Gaetano Pavone
Gaetano Pavone 2020년 7월 7일
답변: Gaetano Pavone 2020년 7월 8일
I have the following code:
cellnumber=6;
Vtype=4;
r=500;
H=500;
d=0.5*r;
nodes = [0 0 r 0 -r 0 sqrt(2)*r/2 -sqrt(2)*r/2 -sqrt(2)*r/2 sqrt(2)*r/2
0 0 0 r 0 -r sqrt(2)*r/2 sqrt(2)*r/2 -sqrt(2)*r/2 -sqrt(2)*r/2
H-d H 0 0 0 0 2*H-d 2*H-d 2*H-d 2*H-d];
totalnodes=size(nodes,2)+(cellnumber-1)*(size(nodes,2)-Vtype);
nodestotal=zeros(3,totalnodes);
for i=1:size(nodes,2)
nodestotal(:,i)=nodes(:,i);
for ii=size(nodes,2)+1:size(nodes,2)-Vtype:totalnodes
nodestotal(1,ii)=nodes(1,1);
nodestotal(2,ii)=nodes(2,1);
nodestotal(3,ii)=?????????;
for jj=size(nodes,2)+2:size(nodes,2)-Vtype:totalnodes
nodestotal(1,jj)=nodes(1,2);
nodestotal(2,jj)=nodes(2,2);
nodestotal(3,jj)=?????????;
end
end
end
I would like to assign specific values to specific elements of nodestotal (indicated as ?????????) in for loop. In particular such values are:
nodestotal(3,11)=3*H-2*d nodestotal(3,12)=3*H-*d
nodestotal(3,17)=5*H-3*d nodestotal(3,18)=5*H-2*d
nodestotal(3,23)=7*H-4*d nodestotal(3,24)=7*H-3*d
nodestotal(3,29)=9*H-5*d nodestotal(3,30)=9*H-4*d
nodestotal(3,35)=11*H-6*d nodestotal(3,36)=11*H-5*d
How can I do this?
Notice that cellnumber can vary.

채택된 답변

Gaetano Pavone
Gaetano Pavone 2020년 7월 8일
This is the solution:
for i=1:size(nodes,2)
nodestotal(:,i)=nodes(:,i);
index1=(size(nodes,2)+1:size(nodes,2)-Vtype:totalnodes);
index2=(size(nodes,2)+2:size(nodes,2)-Vtype:totalnodes);
for ii=size(nodes,2)+1:size(nodes,2)-Vtype:totalnodes
nodestotal(1,ii)=nodes(1,1);
nodestotal(2,ii)=nodes(2,1);
idx1=find(index1==ii);
nodestotal(3,ii)=((2*idx1)+1)*H-(idx1+1)*d;
for jj=size(nodes,2)+2:size(nodes,2)-Vtype:totalnodes
nodestotal(1,jj)=nodes(1,2);
nodestotal(2,jj)=nodes(2,2);
idx2=find(index2==jj);
nodestotal(3,jj)=((2*idx2)+1)*H-idx2*d;
end
end
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by