Can I index a matrix on the same line it's created?
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Alexander Ketzle
el 30 de Nov. de 2021
Comentada: Benjamin Kraus
el 1 de Dic. de 2021
Hi y'all, I'm just trying to see if I can compress a script even further than I already have in hopes of potentially making it one line for no reason other than it'd be funny. Since I can put disp on the same line as clc, clear all; I was wondering if it was possible to index a matrix on the same line it's created, from this code:
clc, clear all;
coordinates = [6,2;-4,8;-5,-1;3,-7];
disp(table(coordinates(:,1),coordinates(:,2),sqrt(coordinates(:,1).^2 + coordinates(:,2).^2),(acosd(coordinates(:,1)./sqrt(coordinates(:,1).^2 + coordinates(:,2).^2)) .* sign(coordinates(:,2))),'VariableNames',{'X coordinate','Y coordinate','Arm Length (in)','Arm Angle (degrees)'}));
while yes I could just input the points manually, I want to see if there's a way to do it and be able to feed in any matrix I want quickly.
Thanks.
0 comentarios
Respuesta aceptada
Benjamin Kraus
el 30 de Nov. de 2021
When I first read your question, I assumed this was not an acceptable answer:
clc, clear all; coordinates = [6,2;-4,8;-5,-1;3,-7]; disp(table(coordinates(:,1),coordinates(:,2),sqrt(coordinates(:,1).^2 + coordinates(:,2).^2),(acosd(coordinates(:,1)./sqrt(coordinates(:,1).^2 + coordinates(:,2).^2)) .* sign(coordinates(:,2))),'VariableNames',{'X coordinate','Y coordinate','Arm Length (in)','Arm Angle (degrees)'}));
However, you said: "Since I can put disp on the same line as clc, clear all", which suggests using semicolons to separate expressons on a single line is acceptable.
Regardles, I think the following accomplishes what you are trying to do, and does it fairly generically:
clc, clear, cellfun(@(coordinates) disp(table(coordinates(:,1),coordinates(:,2),sqrt(coordinates(:,1).^2 + coordinates(:,2).^2),(acosd(coordinates(:,1)./sqrt(coordinates(:,1).^2 + coordinates(:,2).^2)) .* sign(coordinates(:,2))),'VariableNames',{'X coordinate','Y coordinate','Arm Length (in)','Arm Angle (degrees)'})), {[6,2;-4,8;-5,-1;3,-7]})
There may be other ways to leverage the *fun family of functions (arrayfun, rowfun, cellfun, varfun).
Note: you can also do all sorts of tricks using eval, but that seems like it is cheating.
On this topic, you may find this series of blog posts interesting: Introduction to Functional Programming with Anonymous Functions: Part 1, Part 2, Part 3.
As an aside: clear all is overkill for most situations, and it can significantly slow down most scripts. For most purposes, clear is more than enough.
2 comentarios
Benjamin Kraus
el 1 de Dic. de 2021
A quick note about clear: Just clear alone will clear all variables in the workspace. Adding clear all does a lot more, including clearing MATLAB's cached memory of local scripts and functions, persistent variables, global variables, etc. This will slow down future commands, because they've got to rebuild some of that cache. Unless you are regularly using global variables (which is generally considered bad practice) or really need to clear persistant variables (this is more common), clear should be enough.
Más respuestas (0)
Ver también
Categorías
Más información sobre Time Series Objects 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!