필터 지우기
필터 지우기

Multiple lines of similar variable names

조회 수: 3 (최근 30일)
Chris
Chris 2020년 11월 14일
댓글: Peter Perkins 2020년 11월 19일
Hi, I have the following arbitrary MATLAB code. I am just trying to clear outliers out of these matrices but have to repeatedly use the filloutliers command for each individual component. This can become tedious if there are 20+ components to retype over and over, I was wondering if there is a way to perform this task using less lines and less time. Thank you.
A_1 = [1:20, 50]
A_2 = [70:80, 20]
A_3 = [50:60, 2]
B_1 = filloutliers(A_1,'linear');
B_2= filloutliers(A_2,'linear');
B_3 = filloutliers(A_3',linear');
  댓글 수: 1
Stephen23
Stephen23 2020년 11월 15일
편집: Stephen23 2020년 11월 15일
"This can become tedious if there are 20+ components to retype over and over..."
Yes, the approach of using lots of numbered variables should definitely be avoided.
"...I was wondering if there is a way to perform this task using less lines and less time"
Of course it is possible to do this much more efficiently: just use indexing, e.g. with a cell array or an ND numeric array, just as MATLAB was designed for. Using indexing is exactly what the MATLAB documentation reccomends.
Note that numbering variables like that is a sign that you are doing something wrong.
Putting meta-data (such as pseudo-indices) into variable names is a sign that you are doing something very wrong.
By splitting up your data into lots of separate (numbered) variables and then trying to access them you force yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
The MATLAB documentation specifically recommends against your current approach: "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
You should use indexing, just as MATLAB was designed for, just like all experienced MATLAB users do.

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

채택된 답변

ICR
ICR 2020년 11월 14일
Hi
It is not advisable to change the variable names within the loop due to various reasons mentioned in this link:
But if you still want to work with changing the variables:
%Your input data
A_1 = [1:20, 50]
A_2 = [70:80, 20]
A_3 = [50:60, 2]
for i = 1:1:3
result = filloutliers(eval(['A_' num2str(i)]),'linear');
eval(['B_' num2str(i) '= result'])
end
  댓글 수: 2
Chris
Chris 2020년 11월 17일
Thank you!
Peter Perkins
Peter Perkins 2020년 11월 19일
DON'T do that, as Stephen has already explained.
If your vectors were all the same length, you'd put them in a matrix or a table and it would be one line. If they are different lengths, put them in a cell array as Bastian suggests, and either loop as in his code or use cellfun.

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

추가 답변 (1개)

Setsuna Yuuki.
Setsuna Yuuki. 2020년 11월 14일
편집: Setsuna Yuuki. 2020년 11월 14일
Maybe you can use "cell" to store your arrays, then when using "filloutliers" you run through it with a "for" and store it in another cell.
A{1} = [1:20, 50];
A{2} = [70:80, 20];
A{3} = [50:60, 2];
for n=1:3
B{n} = filloutliers(A{n},'linear');
end
More info: https://es.mathworks.com/help/matlab/ref/cell.html

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by