Filter löschen
Filter löschen

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

1 Ansicht (letzte 30 Tage)
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]

Antworten (1)

Walter Roberson
Walter Roberson am 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 Kommentare
sci hub
sci hub am 20 Jul. 2021
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 ???

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by