How to add the rows in a for loop
조회 수: 2 (최근 30일)
이전 댓글 표시
I have the following code,
labels = 1:100;
element_positions = zeros(length(labels-10),4)
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions = [nodes(i,:),nodes(i+1,:);
nodes(i,:),nodes(i+10,:);
nodes(i,:),nodes(i+11,:);
nodes(i+10,:),nodes(i+1,:)]
end
I want to make an element matrix in which I couple the nodes to a full matrix, instead of computing 9 indepentendt matrices, how do i make a for loop that adds them?
I already tried
labels = 1:100;
element_positions = zeros(length(labels-10),4)
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions(i,:) = [nodes(i,:),nodes(i+1,:);
nodes(i,:),nodes(i+10,:);
nodes(i,:),nodes(i+11,:);
nodes(i+10,:),nodes(i+1,:)]
end
but then it get the errror: Unable to perform assignment because the size of the left side is 1-by-4 and the size of the right side is 4-by-4.
Thanks in advance,
Frank
댓글 수: 2
답변 (2개)
KSSV
2018년 11월 16일
Should be something like this to solve error.
labels = 1:100;
element_positions = zeros(length(labels-10),4)
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions(i,:) = [nodes(i,1),nodes(i+1,2);
nodes(i,2),nodes(i+10,3);
nodes(i,3),nodes(i+11,4);
nodes(i+10,4),nodes(i+1,1)]
end
댓글 수: 3
madhan ravi
2018년 11월 16일
labels = 1:100;
element_positions = cell(1,length(labels-10)); %preallocation
ctr=1;
for i = [1:9,11:19,21:29,31:39,41:49,51:59,61:69,71:79,81:89];
element_positions{ctr} = [nodes(i,1),nodes(i+1,2);
nodes(i,2),nodes(i+10,3);
nodes(i,3),nodes(i+11,4);
nodes(i+10,4),nodes(i+1,1)]
ctr=ctr+1;
end
celldisp(element_positions)
[element_positions{:}] %double matrix
댓글 수: 3
참고 항목
카테고리
Help Center 및 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!
