Filter löschen
Filter löschen

Multiple lines of similar variable names

2 Ansichten (letzte 30 Tage)
Chris
Chris am 14 Nov. 2020
Kommentiert: Peter Perkins am 19 Nov. 2020
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 Kommentar
Stephen23
Stephen23 am 15 Nov. 2020
Bearbeitet: Stephen23 am 15 Nov. 2020
"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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

ICR
ICR am 14 Nov. 2020
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 Kommentare
Chris
Chris am 17 Nov. 2020
Thank you!
Peter Perkins
Peter Perkins am 19 Nov. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Setsuna Yuuki.
Setsuna Yuuki. am 14 Nov. 2020
Bearbeitet: Setsuna Yuuki. am 14 Nov. 2020
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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by