Unable to perform assignment because the left and right sides have a different number of elements.
1 view (last 30 days)
Show older comments
Hi all,
I am trying to divide a two line (two times) based on different length as the following code:
but I can not
clear all
clc
t = [2 0.40 1.2];
t_ID = [3 1]; % Each line index
Z = zeros(2+1,1);
for j = 1:2
if (j == 1)
t1 = 0; t2 = t(t_ID(j));
Z(j) = linspace(t1,t2,2);
else
t1 = Z(2*j-1);
t2 = t1+t_av(t_ID(j));
Z(2*j-1:2*j) = linspace(t1,t2,2);
end
end
% Z should be [0 0.2 0.3250]
0 Comments
Answers (1)
Walter Roberson
on 19 Jul 2021
First get rid of the "clear all". Your code relies on variables being in memory, but the clear destroys them.
Z(j) = linspace(t1,t2,2);
linspace requesting two output elements is never going to fit a scalar destination. Also linspace requesting two output elements would just be the vector [t1, t2] so just code that instead of confusing things.
When j becomes 2 then 2*j-1 would be 3 so you would be writing to locations 3 and 4 in the vector. Your code will never create a vector of odd length.
7 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!