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

1 次查看(过去 30 天)
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]

回答(1 个)

Walter Roberson
Walter Roberson 2021-7-19
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 个评论
sci hub
sci hub 2021-7-20
Sounds working, but if I wanna divide the line into more two or more pieces, it will not work.. it will not give the correct data.. no change......
so any suggestions ???

请先登录,再进行评论。

类别

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