Programmatically define elements of a matrix

2 views (last 30 days)
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.

Accepted Answer

Gaetano Pavone
Gaetano Pavone on 8 Jul 2020
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

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!