Multiple lines of similar variable names

2 次查看(过去 30 天)
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 个评论
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