Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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]

Risposte (1)

Walter Roberson
Walter Roberson il 19 Lug 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 Commenti
sci hub
sci hub il 20 Lug 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 ???

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by