フィルターのクリア

Multiple lines of similar variable names

2 ビュー (過去 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
  1 件のコメント
Chris
Chris 2020 年 11 月 17 日
Thank you!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by