Multiple lines of similar variable names
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Chris
el 14 de Nov. de 2020
Comentada: Peter Perkins
el 19 de Nov. de 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 comentario
Stephen23
el 15 de Nov. de 2020
Editada: Stephen23
el 15 de Nov. de 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.
Respuesta aceptada
ICR
el 14 de Nov. de 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 comentarios
Peter Perkins
el 19 de Nov. de 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.
Más respuestas (1)
Setsuna Yuuki.
el 14 de Nov. de 2020
Editada: Setsuna Yuuki.
el 14 de Nov. de 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
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!