Matlab Error after executing the code
조회 수: 1 (최근 30일)
이전 댓글 표시
Dear sir/Madam
I have the following Matlab code.
clear all
clc
kc=[1 2 3 4 5 6 7 8 9 0];
k12=200;
k34=300;
new1=1:1:10;
for h1=1:length(new1);
for l1=1:length(new1);
K(h1,l1)=[k12 -k12 0 0;-k12 kc(h1,l1)+k12 -kc(h1,l1) 0;0 -kc(h1,l1) k34+kc(h1,l1) -k34;0 0 -k34 k34];
end
end
After executing it, an error is displayed as "Unable to perform assignment because the left and right sides have a different number of elements." Kindly provide any suggestions to avoid this error.
댓글 수: 2
Sreedevi K
2021년 11월 4일
You are trying to save a 4*4 matrix (RHS) into a single element (LHS) which is not allowed.
답변 (2개)
Sreedevi K
2021년 11월 4일
You have to save into a cell array or a 3D matrix. As shown below:
kc=[1 2 3 4 5 6 7 8 9 0];
k12=200;
k34=300;
new1=1:1:10;
count = 0 ;
for h1=1:length(new1)
for l1=1:length(new1)
count = count+1 ;
K{count}=[k12 -k12 0 0;-k12 kc(h1,l1)+k12 -kc(h1,l1) 0;0 -kc(h1,l1) k34+kc(h1,l1) -k34;0 0 -k34 k34];
end
end
댓글 수: 0
VBBV
2021년 11월 30일
clear all
clc
kc=[1 2 3 4 5 6 7 8 9 0];
k12=200;
k34=300;
new1=1:1:10;
for l1=1:length(new1)
K(:,:,:,:,l1)=[k12 -k12 0 0;-k12 kc(l1)+k12 -kc(l1) 0;0 -kc(l1) k34+kc(l1) -k34;0 0 -k34 k34];
end
K
clear K
TT = 1
for l1=1:length(new1)
K{TT}=[k12 -k12 0 0;-k12 kc(l1)+k12 -kc(l1) 0;0 -kc(l1) k34+kc(l1) -k34;0 0 -k34 k34];
TT = TT+1;
end
K
You need one for loop only to compute the matrix, Also, you were getting error because, when you access more elements from matrix than its actual dimension. You can do it using cell array as below
댓글 수: 0
참고 항목
카테고리
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!