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]
댓글 수: 0
답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!