Index exceeds the number of array elements (0) error. Help please
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi,
Below is my very simple code. What is wrong with this. Could you help me please?
function [E]=mainy()
clc
clear all
syms x t eta zeta xsi m tau epsilon k
for j=1:3
WK=[];
w(j)=j*2*x
end
for k=1:3
UXT=[];
UXT(k)=k*x
end
for i=1:3
E=[];
E=UXT(i)-WK(i)
% E=(UXT(k)-WK(k))/(UXT(k))
end
end
채택된 답변
Walter Roberson
2021년 3월 3일
편집: Walter Roberson
2021년 3월 3일
It is not advisable to use clc inside most functions. There is not typically a reason to clear the command window.
You should never "clear all" inside a function. If you use "clear all" at all, it should only be inside one script that you use to reset the state of MATLAB when you switch tasks. "clear all" inside a function is like Wyle E. Coyote blowing up the bridge he is standing on.
Setting a variable to [] in every iteration of a loop is not productive in most cases. If the variable is not being set to something else inside the loop before being set to [] then you are just wasting time.
Every iteration of for j you clear all of WK, making it into an array of size 0. Then in for i you try to access WK(1) on the first iteration, but offset (1) is not there because you wrote [] to WK (multiple times)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!