필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
I keep getting the error 'Unable to perform assignment because the left and right sides have a different number of elements.' whenever I try to run it, and I'm not quite sure why as according to the workspace, all elements have the same size/value. Any help fixing this would be appreciated!
h = 0.08;
t = 0:h:2;
x = zeros(size(t));
x(1) = 2/25;
n = numel(x);
f = 5*(x-t.^2);
sz_x = size(x);
sz_f = size(f);
for i = 1:n-1
f = 5*(x-t.^2);
x(i+1) = x(i) + h*f;
end
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 12월 8일
f is a vector, thus the RHS of the assignment is a vector, and you are trying to fit a vector into a scalar placeholder, which results in the error you get.
x(i+1) = x(i) + h*f;

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 12월 8일
This version of the code does more work than is necessary, but has the minimal change needed to prevent the particular error you were encountering.
h = 0.08;
t = 0:h:2;
x = zeros(size(t));
x(1) = 2/25;
n = numel(x);
f = 5*(x-t.^2);
sz_x = size(x);
sz_f = size(f);
for i = 1:n-1
f = 5*(x-t.^2);
x(i+1) = x(i) + h*f(i);
end
plot(t, x)

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by