If I want to change the middle element of the array n, from n1 to n6, step wise, what code has to be used ?
조회 수: 5 (최근 30일)
이전 댓글 표시
댓글 수: 1
Stephen23
2020년 7월 23일
Numbering variables like that is a sign that you are doing something wrong.
The simple, neat, efficient solution is to just use idnexing.
답변 (1개)
Rik
2020년 7월 23일
If you replace n1 with n_{1} (and do the same for the other 5), you can use a loop.
Don't use numbered variables, use arrays instead.
Also, don't use clear all (use clear or clearvars to keep your workspace clean during debugging), and avoid close all if you don't plot anything.
댓글 수: 3
Stephen23
2020년 7월 23일
That is not how MATLAB loop variables work. To know how to use for loops in MATLAB read the documentation:
Usually the simplest and most robust approach is to loop over indices.
Rik
2020년 7월 23일
To continue on what Stephen mentioned: you probably need something like the code below (and next time post code as code, not as an image).
for k=1:6
temp=n_{k};
%do something with temp
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Environment and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!