Unable to perform assignment because the left and right sides have a different number of elements.

조회 수: 199 (최근 30일)
I'm trying to solve this problem in many ways but there is still the error like in the tittle
Script:
n1=1.35
y=0:0.01:0.8;
T3=zeros(length(y),1);
for a=1:length(y)
y(a)=y(a)/n1;
T3(a)=[1 y(a); 0 1];
end
  댓글 수: 1
Hector
Hector 2023년 3월 21일
편집: Steven Lord 2023년 3월 21일
How about assign y(i) + [(3*(Q/A).*sind(t).^2)-(Q/A)].*0.5 (size 21 x 1) to y(i) (size 1) with different number of elements? It was effective in my situation when I was in [SL: removed link that from the URL looked like spam] You will soon find the useful way. Good luck!

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2019년 1월 9일
The left side of this line of code refers to 1 element of T3. The right side refers to a 4 element matrix.
T3(a)=[1 y(a); 0 1];
You can't stuff a 4 element matrix into 1 element of a vector. It works about as well as stuffing 4 eggs into one of the cups in an egg carton without breaking any.
You could create a 3-dimensional array where each page of the array contains one of these matrices or you could create a cell array.

추가 답변 (1개)

Ali Syed
Ali Syed 2021년 1월 30일
편집: DGM 2023년 2월 13일
The notion to use for making both sides sizes equal to each other will be as follows in the example:
% It is MARTIX_NAME( :, :, i) = MATRIX; %Where the i is the incremental chnage to go to the next value
B = [0, 1, 0, pi, pi, pi/2, 0, 0, 1, pi/2, pi, 1, 1]
RANDOM_Var = ones (4,4)
for i=1:13
T = B(:, i)
C = cos(T)
S = sin(T)
k = [C, S, C*S, S; C, C, S, S; C*S, C*S, C^2, S^2; C^2, S^2, S, C]
RANDOM_Var(:, :, i) = k
end
  댓글 수: 5
Ahmad
Ahmad 2024년 2월 6일
편집: Walter Roberson 2024년 2월 7일
I'm encountering the same problem in line 12 of my code where I'm trying to plot a piecewise continuous graph in its discrete time form. I'm new to Matlab and I'm trying to learn, any help would be greatly appreciated as I honestly don't know what's wrong or how to solve it. I also have the question which I can add as well if it helps.
T=4*pi;
i=10;
h=T/i;
a=1;
tcs=0;
tcm=T/2;
tce=T;
for ts1=tcs:h:tcm
t(a)=ts1;
t1(a)=ts1-tcs;
y(a)=1-exp(-t1);
a=a+1;
ts1=ts1+h;
end
Unable to perform assignment because the left and right sides have a different number of elements.
for ts2=tcm:h:tce
t(a)=ts2;
t1(a)=ts2-tcm;
y(a)=exp(-t2);
a=a+1;
ts2=ts2+h;
end
plot(t,y)
Stephen23
Stephen23 2024년 2월 7일
One this line you are trying to force a vector into a scalar location:
y(a)=1-exp(-t1);
On the first loop iteration that will work because t1 is also scalar. But after that you will get an assignment error. The solution is exactly the same as with the other discussions on this thread: you need to slect the same number of elements on the RHS as you are trying to assign on the LHS of that assignment:
y(a)=1-exp(-t1(a));
% ^^^
Your code has some other bugs.

댓글을 달려면 로그인하십시오.

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by