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

1,594 views (last 30 days)
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 Comment
Hector
Hector on 21 Mar 2023
Edited: Steven Lord on 21 Mar 2023
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!

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 9 Jan 2019
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.

More Answers (1)

Ali Syed
Ali Syed on 30 Jan 2021
Edited: DGM on 13 Feb 2023
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 Comments
Ahmad
Ahmad on 6 Feb 2024
Edited: Walter Roberson on 7 Feb 2024
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 on 7 Feb 2024
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.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!